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