使用CodeDOM动态编译一个字符串表达式

由于程序需要,计算的表达式使用字符串传输,这样对运算造成了影响。在程序中直接执行这段表达式可以得到值,

但是使用字符串就没有办法运算了,

所以想到用CodeDOM将这段字符串拼接在代码中编译

类似string str="1>0";  需要返回True,同样也是用这种方式

 1 using System;
 2 using System.CodeDom.Compiler;
 3 using System.Reflection;
 4 using System.Text;
 5 using Microsoft.CSharp;
 6 
 7 namespace Test
 8 {
 9     public class Program
10     {
11         static void Main(string[] args)
12         {
13             string text ="Math.Pow(Math.Sqrt(((2.08 * 0.2 + 1.82 * 0.2 + 2.02 * 0.2 + Math.Sqrt(3.08) * 0.1 + 2.18 * 0.1 + Math.Log10(2.13 * 0.1 + 2.13 * 0.1)) / 5) * 100),3)";
14 
15             // 1.CSharpCodePrivoder
16             CSharpCodeProvider objCSharpCodePrivoder = new CSharpCodeProvider();
17 
18             // 2.ICodeComplier
19             ICodeCompiler objICodeCompiler = objCSharpCodePrivoder.CreateCompiler();
20 
21             // 3.CompilerParameters
22             CompilerParameters objCompilerParameters = new CompilerParameters();
23             objCompilerParameters.ReferencedAssemblies.Add("System.dll");
24             objCompilerParameters.GenerateExecutable = false;
25             objCompilerParameters.GenerateInMemory = true;
26 
27             // 4.CompilerResults
28             CompilerResults cr = objICodeCompiler.CompileAssemblyFromSource(objCompilerParameters, GenerateCode(text));
29 
30             if (cr.Errors.HasErrors)
31             {
32                 Console.WriteLine("编译错误:");
33                 foreach (CompilerError err in cr.Errors)
34                 {
35                     Console.WriteLine(err.ErrorText);
36                 }
37             }
38             else
39             {
40                 // 通过反射,调用HelloWorld的实例
41                 Assembly objAssembly = cr.CompiledAssembly;
42                 object objHelloWorld = objAssembly.CreateInstance("DynamicCodeGenerate.HelloWorld");
43                 MethodInfo objMI = objHelloWorld.GetType().GetMethod("OutPut");
44 
45                 Console.WriteLine(objMI.Invoke(objHelloWorld, null));
46             }
47 
48             Console.ReadLine();
49         }
50 
51         static string GenerateCode(string abc)
52           {
53               StringBuilder sb = new StringBuilder();
54               sb.Append("using System;");
55               sb.Append(Environment.NewLine);
56               sb.Append("namespace DynamicCodeGenerate");
57               sb.Append(Environment.NewLine);
58               sb.Append("{");
59               sb.Append(Environment.NewLine);
60               sb.Append("      public class HelloWorld");
61               sb.Append(Environment.NewLine);
62               sb.Append("      {");
63               sb.Append(Environment.NewLine);
64               sb.Append("          public string OutPut()");
65               sb.Append(Environment.NewLine);
66               sb.Append("          {");
67               sb.Append(Environment.NewLine);
68               sb.Append(string.Format("               return {0}+"";",abc));
69               sb.Append(Environment.NewLine);
70               sb.Append("          }");
71               sb.Append(Environment.NewLine);
72               sb.Append("      }");
73               sb.Append(Environment.NewLine);
74               sb.Append("}");
75 
76             string code = sb.ToString();
77               Console.WriteLine(code);
78               Console.WriteLine();
79 
80             return code;
81           }
82     }
83 }
代码
原文地址:https://www.cnblogs.com/codeyou/p/4955898.html