简体   繁体   中英

Compile C# code extension at runtime

I have a system that compiles C# code at runtime. I would like the generated assemblies to be linked to the system itself. Here's some example code that I am using:

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);

What I would like to do is also add a reference to the main compiler program as well, so the newly compiled extension can use library routines from the compiler program.

Assembly entryasm = Assembly.GetEntryAssembly();

So the question is this: How do I add a reference to entryasm in the compiled Assembly result?

你试过这个吗?

   compilerparams.ReferencedAssemblies.Add(entryasm.Location);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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