我猜它既不调用csc.exe,也没有实现整个编译器,那它是如何工作的呢?
更新:感谢Jon Skeet指向易于学习的代码。
更新:感谢Jon Skeet指向易于学习的代码。
string c = @"
public class A
{
public static void Main(string[] args)
{
System.Console.WriteLine(""hello world"");
}
}
";
CodeDomProvider compiler = new CSharpCodeProvider();
CompilerParameters parameters = new CompilerParameters();
parameters.WarningLevel = 4;
parameters.GenerateExecutable = false;
parameters.GenerateInMemory = true;
CompilerResults r = compiler.CompileAssemblyFromSource(parameters, c);
Assembly a = r.CompiledAssembly;
Type[] ts = a.GetTypes();
Type t = ts[0];
object o = t.GetMethod("Main").Invoke(null, new object[] { new string[] { } });