我制作了一个用于学习/实验的玩具编译器。
下面是编译一些 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在运行时生成的类型进行通信?
具体来说,我对订阅公共委托或捕获标准输出感兴趣,但是关于类型如何在这种情况下相互通信的一般信息就足够了。