3 回答

TA貢獻(xiàn)1815條經(jīng)驗(yàn) 獲得超6個(gè)贊
一種不建議使用的方法(使用.NET 4.0作為前面提到的海報(bào)):
using System.CodeDom.Compiler;
using System.Reflection;
using System;
public class J
{
public static void Main()
{
System.CodeDom.Compiler.CompilerParameters parameters = new CompilerParameters();
parameters.GenerateExecutable = false;
parameters.OutputAssembly = "AutoGen.dll";
CompilerResults r = CodeDomProvider.CreateProvider("CSharp").CompileAssemblyFromSource(parameters, "public class B {public static int k=7;}");
//verify generation
Console.WriteLine(Assembly.LoadFrom("AutoGen.dll").GetType("B").GetField("k").GetValue(null));
}
}

TA貢獻(xiàn)1816條經(jīng)驗(yàn) 獲得超6個(gè)贊
現(xiàn)在,最好的選擇是CSharpCodeProvider;4.0的計(jì)劃包括“編譯器即服務(wù)”,它將對(duì)此進(jìn)行完全管理。
- 3 回答
- 0 關(guān)注
- 603 瀏覽
添加回答
舉報(bào)