C # dynamic compilation code

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.