Interacting with assembly generated by CSharpCodeProvider

徘徊边缘 提交于 2019-12-11 18:28:39

问题


I made a toy compiler for learning/experimentation.

Here's the code for compiling some C# code:

using System;
using System.CodeDom.Compiler;
using System.Reflection;
using Microsoft.CSharp;

namespace ProgrammingSystem
{
    public class Compiler
    {
        public Program Compile(Code code)
        {
            string template = @"
public class C 
{
    public static void Main(string[] args) 
    { 
        [[Source]] 
    } 
}";

            string source = template.Replace("[[Source]]", code.Source);

            CodeDomProvider compiler = new CSharpCodeProvider();

            CompilerParameters parameters = new CompilerParameters();
            parameters.WarningLevel = 4;
            parameters.GenerateExecutable = false;
            parameters.GenerateInMemory = true;

            CompilerResults r = compiler.CompileAssemblyFromSource(parameters, source);
            foreach (var item in r.Output)
                this.Fire(Output, item);            

            Assembly assembly = r.CompiledAssembly;

            return new Program(assembly);
        }

        public event EventHandler<OutputEventArgs> Output;
    }
}

And here's the code for running the copmiled code:

using System;
using System.Reflection;

namespace ProgrammingSystem
{
    public class Program
    {
        private System.Reflection.Assembly assembly;

        public Program(System.Reflection.Assembly assembly)
        {
            this.assembly = assembly;
        }

        public event EventHandler<OutputEventArgs> ProgramOutput;

        public void Run()
        {
            Type[] types = this.assembly.GetTypes();
            Type programType = types[0];
            MethodInfo programMainMethod = programType.GetMethod("Main");
            programMainMethod.Invoke(null, new object[] { new string[] { } });

            this.Fire(ProgramOutput, "hello");
        }
    }
}

By what means may I communicate with the type that is produced at runtime by the CSharpCodeProvider?

Specifically I'm interested in either subscribing to a public delegate or capturing the standard output but general information on how types can talk to eachother in a situation like this would suffice.


回答1:


I figured this out using documentation here.

using System;
using System.Reflection;

namespace ProgrammingSystem
{
    public class Program
    {
        private System.Reflection.Assembly assembly;

        public Program(System.Reflection.Assembly assembly)
        {
            this.assembly = assembly;
        }

        public event EventHandler<OutputEventArgs> ProgramOutput;

        public void Run()
        {
            Type programType = this.assembly.GetType("C");
            object program = Activator.CreateInstance(programType);
            EventInfo outputEvent = programType.GetEvent("output");
            Delegate delegateInstance = Delegate.CreateDelegate(
                                                    outputEvent.EventHandlerType, this,
                                                    typeof(Program).GetMethod("OutputHandler", BindingFlags.NonPublic | BindingFlags.Instance));
            MethodInfo addHandlerMethod = outputEvent.GetAddMethod();
            addHandlerMethod.Invoke(program, new [] { delegateInstance });

            programType.GetMethod("Run").Invoke(program, null);
        }

        private void OutputHandler(string s)
        {
            this.Fire(ProgramOutput, s);
        }
    }
}

using System;
using System.CodeDom.Compiler;
using System.Reflection;
using Microsoft.CSharp;

namespace ProgrammingSystem
{
    public class Compiler
    {
        public Program Compile(Code code)
        {
            string template = @"
using System;
public class C 
{
    public event Output output;
    public void Run() 
    { 
        [[Source]]
        output(""end"");
    }
}
public delegate void Output(string s);
";

            string source = template.Replace("[[Source]]", code.Source);

            CodeDomProvider compiler = new CSharpCodeProvider();

            CompilerParameters parameters = new CompilerParameters();
            parameters.WarningLevel = 4;
            parameters.GenerateExecutable = false;
            parameters.GenerateInMemory = true;

            CompilerResults r = compiler.CompileAssemblyFromSource(parameters, source);
            foreach (var item in r.Output)
                this.Fire(Output, item);            

            Assembly assembly = r.CompiledAssembly;

            return new Program(assembly);
        }

        public event EventHandler<OutputEventArgs> Output;
    }
}


来源:https://stackoverflow.com/questions/12648290/interacting-with-assembly-generated-by-csharpcodeprovider

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!