C# 对动态编辑的一些学习笔记

  1 using System;
  2 using System.Collections.Generic;
  3 using System.Linq;
  4 using System.Text;
  5 
  6 namespace CompilerTest
  7 {
  8     // Compiler
  9     class MyCompilter
 10     {
 11         // Provider
 12         private Microsoft.CSharp.CSharpCodeProvider Provider;
 13         // CompilerParameters
 14         private System.CodeDom.Compiler.CompilerParameters cps;
 15         // Default Libs
 16         public string[] defaultLibs = { "System.dll" };
 17 
 18         // Singleton
 19         private static MyCompilter instance = null;
 20         private MyCompilter() {
 21             Provider = new Microsoft.CSharp.CSharpCodeProvider();
 22         }
 23         public static MyCompilter Instance() {
 24             if (instance == null) {
 25                 instance = new MyCompilter();
 26             }
 27             return instance;
 28         }
 29 
 30         // CompilerParameters
 31         private void CreateCps(string[] libs) {
 32             // 编译参数
 33             cps = new System.CodeDom.Compiler.CompilerParameters();
 34             // 是否生成 Exe 文件
 35             cps.GenerateExecutable = false;
 36             // 生成在内存中
 37             cps.GenerateInMemory = true;
 38             // 添加引用
 39             cps.ReferencedAssemblies.AddRange(defaultLibs);
 40             if (libs != null) {
 41                 cps.ReferencedAssemblies.AddRange(libs); 
 42             }
 43 
 44             // 将 生成的 的 Dll 文件,保存到硬盘中
 45             cps.OutputAssembly = "d:/tmp.dll";
 46         }
 47 
 48         // Eval String
 49         public System.Reflection.Assembly Compile(string[] Sources, string[] libs = null) {
 50             // Compiler Results
 51             System.CodeDom.Compiler.CompilerResults cr;
 52             // Create Compiler Parameters
 53             CreateCps(libs);
 54             // 开始编译
 55             cr = Provider.CompileAssemblyFromSource(cps, Sources);
 56 
 57             // 如果没有错误的话. 将 生成的 Assembly 返回
 58             if (cr.Errors.Count == 0) {
 59                 return cr.CompiledAssembly;
 60             }
 61             return null;
 62         }
 63     }
 64 
 65     class Program
 66     {
 67         static void Main(string[] args) {
 68             string code = @"
 69                 using System;
 70 
 71                 class ExpressionCalculate
 72                 {
 73                     public void Calculate()
 74                     {
 75                         Console.WriteLine(""kaoooooo,asdfl"");
 76                     }
 77                 }
 78             ";
 79             string code2 = @"
 80                 using System;
 81 
 82                 class ExpressionCalculate2
 83                 {
 84                     public void Calculate2()
 85                     {
 86                         Console.WriteLine(""kaooortrtrtrtooo,atytytysdfl"");
 87                     }
 88                 }
 89             ";
 90 
 91             // 对字符串,进行编译
 92             System.Reflection.Assembly type = MyCompilter.Instance().Compile(new string[] { code,code2 });
 93             // 通过,编译后得到的 Assembly 反射创建其中的一个类,得到一个 Object 类型的对象 
 94             object obj = type.CreateInstance("ExpressionCalculate2");
 95             // 通过 这个 Obj 的 GetType().GetMethod() 方法 按名称得到它的里面的方法
 96             System.Reflection.MethodInfo method = obj.GetType().GetMethod("Calculate2");
 97             // 调用这个方法
 98             method.Invoke(obj, null);
 99         }
100     }
101 }
原文地址:https://www.cnblogs.com/easyfrog/p/3170186.html