簡體   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