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 { public static void Main() { " + "Console.WriteLine(\"Hello, 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 = @" using 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"); } } }
,相关文章
- C#中 如何执行带GO的sql 语句
- C#弹出对话框
- jQuery Validate 表单验证插件----通过name属性来关联字段来验证,改变默认的提示信息,将校验规则写到 js 代码中
- c#调用API的CreateFontIndirect设置字体,编译提示“值不在预期范围”求解?
- C#学习笔记:控件的字体(Font)属性设置
- 基于 Vue.js 之 iView UI 框架非工程化实践记要 使用 Newtonsoft.Json 操作 JSON 字符串 基于.net core实现项目自动编译、并生成nuget包 webpack + vue 在dev和production模式下的小小区别 这样入门asp.net core 之 静态文件 这样入门asp.net core,如何
- iOS编译自动升级版本号脚本
- 如何从文件夹中获取C#中的文件位置
- 如何获取此地图代码的当前位置?当我打开地图时,还试图从服务器接收车辆数据
- C# 实现图片的放大缩小和平移