Using system;
Using Microsoft. CSHARP;
Using system. codedom. compiler;
Using system. codedom;
Namespace test. Cui
{
Class Program
{
Static void main ()
{
// Create a compiler object
Csharpcodeprovider P = new csharpcodeprovider ();
Icodecompiler cc = P. createcompiler ();
// Set compilation Parameters
Compilerparameters Options = new compilerparameters ();
Options. referencedassemblies. Add ("system. dll ");
Options. generateexecutable = true;
Options. outputassembly = "helloworld.exe ";
// Options. referencedassemblies. Add ("system. Windows. Forms. dll ");
// Options. embeddedresources. Add ("data. xml"); // Add built-in Resources
// Options. compileroptions + = "/Target: winexe ";
// Options. compileroptions + = "/Res: resource1.res ";
// Options. compileroptions + = "/win32icon: Test. ICO ";
// Create source code
// 1. Use codedom to create source code
// Codecompileunit Cu = new codecompileunit ();
// Codenamespace samples = new codenamespace ("samples ");
// Cu. namespaces. Add (samples );
// Samples. Imports. Add (New codenamespaceimport ("system "));
// Codetypedeclaration class1 = new codetypedeclaration ("class1 ");
// Samples. types. Add (class1 );
// Codeentrypointmethod start = new codeentrypointmethod ();
// Codemethodinvokeexpression CS1 = new codemethodinvokeexpression (
// New codetypereferenceexpression ("system. Console"), "writeline ",
// New codeprimitiveexpression ("Hello world! "));
// Start. Statements. Add (New codeexpressionstatement (CS1 ));
// Class1.members. Add (start );
// 2. Specify the source code string directly
String code = @"
Using system;
Namespace Samples
{
Public class class1
{
Static void main (string [] ARGs)
{
Console. writeline ("" Hello, world! "");
Console. writeline (datetime. Now. tostring ());
}
}
}
";
Codesnippetcompileunit Cu = new codesnippetcompileunit (CODE );
// Start Compilation
Compilerresults Cr = cc. compileassemblyfromdom (options, Cu );
// Display compilation Information
If (Cr. errors. Count = 0)
Console. writeline ("\" {0} \ "compiled OK! ", CR. compiledassembly. Location );
Else
{
Console. writeline ("complie error :");
Foreach (compilererror error in Cr. Errors)
Console. writeline ("{0}", error );
}
Console. writeline ("Press ENTER key to exit ...");
Console. Readline ();
}
}
}
In addition to the compileassemblyfromdom method, icodecompiler also includes:
Compileassemblyfromdom
Use the specified compiler settings to compile the Assembly from the system. codedom tree contained in the specified codecompileunit.
Compileassemblyfromdombatch
Use the specified compiler to compile the Assembly based on the system. codedom tree in the specified array contained in the codecompileunit object.
Compileassemblyfromfile
Use the specified compiler to compile the Assembly from the source code contained in the specified file.
Compileassemblyfromfilebatch
Use the specified compiler to compile the Assembly from the source code contained in the specified file.
Compileassemblyfromsource
Use the specified compiler to compile an assembly from a specified string that contains the source code.
Compileassemblyfromsourcebatch
From the specified array containing the source code string, use the specified compiler to set the compilation assembly.
So we can easily compile a complex assembly.