Compiling C # code

Source: Internet
Author: User

Using microsoft.csharp;using system.codedom.compiler;using system.reflection;using system.text;namespace System{                public static class Compilecscatruntime {public static void HelloWorld () {String code = @ "                Using System; Namespace first {public class program {Publi c static void Main () {"+" Console.WriteLine (\ "H                          Ello, world!\ ");            + @"                        }                    }                }            ";            CSharpCodeProvider Provider = new CSharpCodeProvider ();            CompilerParameters parameters = new CompilerParameters (); Reference to System.Drawing library parameters.            Referencedassemblies.add ("System.Drawing.dll"); True-memory generation, false-external file generation parameters.            GenerateInMemory = true; True-exe file generation, False-dll file generation parameters.            GenerateExecutable = true;            CompilerResults results = provider.compileassemblyfromsource (parameters, code); if (results.                errors.haserrors) {StringBuilder sb = new StringBuilder (); foreach (CompilerError Error in results. Errors) {sb. Appendline (String.Format ("error ({0}): {1}", error.) ErrorNumber, error.                ErrorText)); } throw new InvalidOperationException (sb.)            ToString ());            } Assembly Assembly = results.compiledassembly; Type program = assembly.            GetType ("First.program"); MethodInfo main = program.            GetMethod ("Main"); Main.        Invoke (null, NULL);            public static void Testmeothds () {MethodInfo function = Createfunction ("x + 2 * y"); var betterfunction = (func<doUble, double, double>) Delegate.createdelegate (typeof (Func<double, Double, double>), function);            Func<double, double, double> lambda = (x, y) + x + 2 * y;            DateTime start;            DateTime stop;            Double result;            int repetitions = 5000000;            start = DateTime.Now;            for (int i = 0; i < repetitions; i++) {result = Originalfunction (2, 3);            } stop = DateTime.Now; Console.WriteLine ("Original-time: {0} ms", (Stop-start).            TotalMilliseconds);            start = DateTime.Now; for (int i = 0; i < repetitions; i++) {result = (double) function.            Invoke (NULL, new object[] {2, 3});            } stop = DateTime.Now; Console.WriteLine ("Reflection-time: {0} ms", (Stop-start).            TotalMilliseconds);            start = DateTime.Now;       for (int i = 0; i < repetitions; i++) {         result = Betterfunction (2, 3);            } stop = DateTime.Now; Console.WriteLine ("Delegate-time: {0} ms", (Stop-start).            TotalMilliseconds);            start = DateTime.Now;            for (int i = 0; i < repetitions; i++) {result = Lambda (2, 3);            } stop = DateTime.Now; Console.WriteLine ("Lambda-time: {0} ms", (Stop-start).        TotalMilliseconds);        public static double Originalfunction (double x, double y) {return x + 2 * y; } public static MethodInfo Createfunction (String function) {String code = @ "Usin                            G System;                    Namespace Userfunctions {public class Binaryfunction                            {public static double Function (double x, double y) { return funC_xy;            }                    }                }            "; String finalcode = code.            Replace ("Func_xy", function);            CSharpCodeProvider Provider = new CSharpCodeProvider ();            CompilerResults results = Provider.compileassemblyfromsource (new CompilerParameters (), Finalcode);            Type binaryfunction = Results.CompiledAssembly.GetType ("userfunctions.binaryfunction");        Return Binaryfunction.getmethod ("Function"); }    }}

  

Compiling C # 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.