Http://www.cnblogs.com/zlgcool/archive/2008/10/12/1309616.html
Before you begin, familiarize yourself with several classes and some of the properties, methods: CSharpCodeProvider, ICodeCompiler, CompilerParameters, CompilerResults, Assembly.
First, CSharpCodeProvider
Provides access to the C # code generator and instances of the code compiler. If you want to dynamically generate VB code, you can use VBCodeProvider.
CreateCompiler (): Gets the instance of the compiler.
Second, ICodeCompiler
Defines the interface used to invoke source code compilation or the CodeDom tree using the specified compiler. Each compilation method accepts the CompilerParameters object that instructs the compiler and returns the CompilerResults object that indicates the result of the compilation.
Compilerassemblyfromsource (compilerparameters option, string source): compiles the assembly from the string containing the source code, using the specified compiler.
Third, CompilerParameters
Represents a parameter that is used to invoke the compiler.
ReferencedAssemblies: Gets the assembly referenced by the current project. The Add method adds a reference to the assembly.
GenerateExecutable: Gets or sets a value that indicates whether the executable file is built. If this property is False, the DLL is generated and the default is False.
GenerateInMemory: Gets or sets a value that indicates whether the output is generated in memory.
Iv. CompilerResults
Represents the compilation result returned from the compiler.
compiledassembly: Gets or sets the assembly, assembly type, to compile.
Wu, Assembly
is the assembly (I don't know how to describe it).
After a rough overview of the above knowledge, you can use C # to dynamically compile and execute code, which is a sample program:
1 usingSystem;2 usingSystem.Reflection;3 usingSystem.Globalization;4 usingMicrosoft.csharp;5 usingsystem.codedom;6 usingSystem.CodeDom.Compiler;7 usingSystem.Text;8 9 namespaceConsoleApplication1Ten { One Public class Program A { - Static voidMain (string[] args) - { the //1.CSharpCodePrivoder -CSharpCodeProvider Objcsharpcodeprivoder =NewCSharpCodeProvider (); - - //2.ICodeComplier +ICodeCompiler Objicodecompiler =Objcsharpcodeprivoder.createcompiler (); - + //3.CompilerParameters ACompilerParameters objcompilerparameters =NewCompilerParameters (); atOBJCOMPILERPARAMETERS.REFERENCEDASSEMBLIES.ADD ("System.dll"); -Objcompilerparameters.generateexecutable =false; -Objcompilerparameters.generateinmemory =true; - - //4.CompilerResults -CompilerResults CR =Objicodecompiler.compileassemblyfromsource (Objcompilerparameters, Generatecode ()); in - if(CR. Errors.haserrors) to { +Console.WriteLine ("Compile Error:"); - foreach(CompilerError ErrinchCr. Errors) the { * Console.WriteLine (Err. ErrorText); $ }Panax Notoginseng } - Else the { + //invoking an instance of HelloWorld through reflection AAssembly objassembly =cr.compiledassembly; the ObjectObjhelloworld = Objassembly.createinstance ("Dynamiccodegenerate.helloworld"); +MethodInfo Objmi = Objhelloworld.gettype (). GetMethod ("OutPut"); - $Console.WriteLine (Objmi.invoke (Objhelloworld,NULL)); $ } - - console.readline (); the } - Wuyi Static stringGeneratecode () the { -StringBuilder SB =NewStringBuilder (); WuSb. Append ("using System;"); - sb. Append (Environment.NewLine); AboutSb. Append ("namespace Dynamiccodegenerate"); $ sb. Append (Environment.NewLine); -Sb. Append ("{"); - sb. Append (Environment.NewLine); -Sb. Append ("Public class HelloWorld"); A sb. Append (Environment.NewLine); +Sb. Append (" {"); the sb. Append (Environment.NewLine); -Sb. Append ("Public string OutPut ()"); $ sb. Append (Environment.NewLine); theSb. Append (" {"); the sb. Append (Environment.NewLine); theSb. Append ("return \ "Hello world!\";"); the sb. Append (Environment.NewLine); -Sb. Append (" }"); in sb. Append (Environment.NewLine); theSb. Append (" }"); the sb. Append (Environment.NewLine); AboutSb. Append ("}"); the the stringCode =sb. ToString (); the Console.WriteLine (code); + Console.WriteLine (); - the returnCode;Bayi } the } the}
. NET Dynamic compilation