C # dynamic Compilation code:
Public auxiliary class, there are comments on the nonsense.
Using system;using system.text;using system.reflection;using system.codedom.compiler;namespace dynamiccompilation.compilation{//////Compile return results//public class Compilationreturn {//// /assembly//public Assembly Assembly {get; set;} Compile Error List//public compilererrorcollection Errors {get; set;} Compile the results if the compilation succeeds Errors==null| | Compilation failed Assembly==null//public Boolean compilationresults {get; set;} }}using system;using system.collections.generic;using system.linq;using system.text;using System.CodeDom.Compiler; namespace dynamiccompilation.compilation{////////C # source operation, mainly including dynamically generated DLLs, dynamic loading DLLs//public class Sourceoperatin G {////////////The DLL is not saved to local/////Pre-loaded DLL///compiled results public static Compilationreturn Sourcecompiler (String strclass, params string[] dllparam) {return SOURCEcompiler (strclass, NULL, Dllparam); ////////////////DLL///DLLs Keep addresses///pre-loaded DLLs//compiled results public static Compilationreturn Sourcecompiler (string strclass, String savepath, params string[] dllparam) { Set the language type to compile CodeDomProvider _p = Codedomprovider.createprovider ("C #"); Compile Parameter object CompilerParameters parameter = new CompilerParameters (); The DLL file that is required for pre-loading the compilation is foreach (String str in dllparam) {parameter. Referencedassemblies.add (str); }//Determine if you need to save the DLL file to the local if (! String.IsNullOrEmpty (Savepath)) parameter. outputassembly = Savepath; Parameter. GenerateExecutable = false; Parameter. GenerateInMemory = false; DLL compiled var _result = _p.compileassemblyfromsource (parameter, strclass); Compilationreturn result = null; IF (_result. Errors.haserrors) {//throws an error return result = new Compilationreturn () {Compilationresults = false, Errors = _result. Errors}; } else {//returns the compiled result to return result = new Compilationreturn () {Compilationresults = true, Assembly = _result. compiledassembly}; } } }}
Dynamically compile the test code, which is a text file:
Using system;using system.text;namespace dynamiccompilation{public class Code {public void Hello () { Console.WriteLine ("This is dynamically compiled");}} }
Test code:
Using system;using system.collections.generic;using system.linq;using system.text;using System.IO;using Dynamiccompilation.compilation;namespace dynamiccompilation{class Program {static void Main (string[] args) {//Read code that needs to be compiled StreamReader objreader = new StreamReader ("Code.txt"); String code = objreader.readtoend (); Dynamic compilation Compilationreturn result = Sourceoperating.sourcecompiler (code, new List () {"System.dll"}). ToArray ()); The IF (!result.compilationresults) {//Compile error throws a compile fault message foreach (var error in result.) Errors) {Console.WriteLine (error. ToString ()); }} else {//instantiate object Dynamic obj = result. Assembly.createinstance ("Dynamiccompilation.code"); The method that invokes the object, obj. Hello (); } } }}
C # dynamic compilation code