繁体   English   中英

在运行时编译C#代码扩展

[英]Compile C# code extension at runtime

我有一个在运行时编译C#代码的系统。 我希望生成的程序集链接到系统本身。 这是我正在使用的一些示例代码:

CSharpCodeProvider provider = new CSharpCodeProvider(new Dictionary<String, String> { { "CompilerVersion", "v3.5" } });
CompilerParameters compilerparams = new CompilerParameters();
compilerparams.GenerateExecutable = false;
compilerparams.GenerateInMemory = true;
foreach (string name in linkedreferences)
    compilerparams.ReferencedAssemblies.Add(name + ".dll");
Assembly result = provider.CompileAssemblyFromFile(compilerparams, filename);

我想要做的还是添加对主编译器程序的引用,因此新编译的扩展可以使用编译器程序中的库例程。

Assembly entryasm = Assembly.GetEntryAssembly();

所以问题是:如何在编译的Assembly结果中添加对entryasm的引用?

你试过这个吗?

   compilerparams.ReferencedAssemblies.Add(entryasm.Location);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM