0

我制作了一个用于学习/实验的玩具编译器。

下面是编译一些 C# 代码的代码:

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;
    }
}

这是运行编译后的代码的代码:

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");
        }
    }
}

我可以通过什么方式与CSharpCodeProvider在运行时生成的类型进行通信?

具体来说,我对订阅公共委托或捕获标准输出感兴趣,但是关于类型如何在这种情况下相互通信的一般信息就足够了。

4

1 回答 1

1

我使用此处的文档解决了这个问题。

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;
    }
}
于 2012-09-28T23:34:54.703 回答