问题
Now this has been bugging me for ages and it's time to find out if it's possible. I currently have a working CodeDom that can compile one class, but I have created a program with multiple classes that I want to compile with CodeDom. I've tried looking here: CodeDom - Linking multiple classes within a single Assembly ^ No help... and here: http://msdn.microsoft.com/en-us/library/system.codedom.compiler.compilerparameters.aspx ^ Not much help...
So, is this possible? And if so, how specifically do I do it? How can I link my classes together and compile?
回答1:
At the top of the CodeDOM tree, there is CodeCompileUnit
, which is roughly equivalent to a single C# source code file. When you use CompileAssemblyFromDom()
to compile, it actually takes a collection of CodeCompileUnit
s. This means you can easily compile multiple classes at once: just create a separate CodeCompileUnit
for each of them.
But that's not the only option, CodeCompileUnit
(just like a C# file) can contain multiple class declarations (indirectly through CodeNamespace
), so you could use that instead.
来源:https://stackoverflow.com/questions/14502819/can-i-compile-multiple-classes-into-one-program-with-codedom