Original http://blog.sunmast.com/sunmast/archive/2004/08/24/870.aspx
/ * * This is a simple codedom example * including code dynamic generation, dynamic compile, and dynamic calls * by sunmast for wangsaokui * 2004.8.23 * / using system; use system.codedom; using system.codedom.compiler; useing system.codedom.com .IO; using System.Reflection; using System.Text; using Microsoft.CSharp; namespace Sunmast.Sample.CodeDom {class Entry {[STAThread] static void Main (string [] args) {// build CodeDOM CodeCompileUnit codeDom = GenerateCodeDom ( ); // // compiled into usable herein Assembly C # code, virtually any inherited classes are Provider CodeDomProvider // row can be replaced Microsoft.VisualBasic.VBCodeProvider Assembly assembly = ComplieCodeDomToAssembly (codeDom, new CSharpCodeProvider ()) ; // dynamic invocation assembly if (assembly = null!) {InvokeMethodsFromAssembly (assembly);} Console.ReadLine ();} static CodeCompileUnit GenerateCodeDom () {// namespace CodeNamespace cn = new CodeNamespace ( "Sunmast.Sample.CodeDom" ); Cn.imports.add (New CodeNameSpaceImport ("system")); // Create a new type of CODETYPEDECLATION CTD = New CodeTypedeclaration ("sunmastclass"); ctd.isclass = true; ctd.typeattrib utes = TypeAttributes.Public; // constructor (not necessarily) CodeConstructor cc = new CodeConstructor (); cc.Attributes = MemberAttributes.Public | MemberAttributes.Final; ctd.Members.Add (cc); // member function MyMethod CodeMemberMethod cmm = new CodeMemberMethod (); cmm.Name = "MyMethod"; cmm.Attributes = MemberAttributes.Public | MemberAttributes.Final; cmm.Parameters.Add (new CodeParameterDeclarationExpression (typeof (string), "input")); cmm.Statements. Add (New CodeMethodInvokeExpression (New Codesnippetexpression ("Console", "WriteLine", New CodeargumentReferenceExpression ("Input"))))
ctd.Members.Add (cmm); cn.Types.Add (ctd); // build compilation unit CodeCompileUnit unit = new CodeCompileUnit (); unit.Namespaces.Add (cn); return unit;} static Assembly ComplieCodeDomToAssembly (CodeCompileUnit compilationUnit , CodeDomProvider languageProvider) {// Code preview Console.WriteLine ( "// Code preview"); StringBuilder sb = new StringBuilder (); ICodeGenerator icg = languageProvider.CreateGenerator (); icg.GenerateCodeFromCompileUnit (compilationUnit, new StringWriter (sb), null); Console.WriteLine (sb.ToString ()); ICodeCompiler complier = languageProvider.CreateCompiler (); // build translation parameters CompilerParameters options = new CompilerParameters (new string [] { "MsCorLib.dll", "System.dll" }); // build in memory Assembly options.GenerateInMemory = true; // Close-tone information, to improve the performance options.IncludeDebugInformation = true; // ignore warning options.TreatWarningsAsErrors = false; // build and compile the results obtained CompilerResults results = Complier.com (options, compilationUnit); foreach (string str in results.output) { Console.writeline (STR);} If (Results.nativeCompilerReturnValue! = 0) {// Compiling Console.Writeline ("! Error:"); foreach (compilererror error in results.erroRORS) {Console.Writeline );} return null;} else {return results.CompiledAssembly;}} static void InvokeMethodsFromAssembly (Assembly assembly) {// get placed Assembly of class // need to write a complete namespace Type t = assembly.GetType ( "Sunmast.Sample .Codedom.sunmastclass ", true, true); // Call the constructor to create object object obj = t.invokemember (null, bindingflags.createInstance, null, null, null); // Object obj = activator.createInstance (T);