asp.net C#获取动态代码的值代码

作者:袖梨 2022-06-25

下面是调用(注意调用时特殊字符的转义)

1 color coor = csharpcodevaluehelper.getcodevalue("color.fromargb(100,100,100)");
2
3
4 image img = csharpcodevaluehelper.getcodevalue("image.fromfile("c:tu.jpg")");

代码如下

  1 usingmicrosoft.csharp;
  2 usingsystem.reflection;
  3  using system.codedom.compiler;
  4
  5 public static class csharpcodevaluehelper
  6     {
  7         ///
  8         /// 编译器实例
  9         ///

10         private static csharpcodeprovider csharpcodeprovider { get; set; }
11
12         ///
13         /// 编译器参数
14         ///

15         private static compilerparameters compilerparameters { get; set; }
16
17         ///
18         /// 构造函数
19         ///

20         static csharpcodevaluehelper()
21         {
22             csharpcodeprovider = new csharpcodeprovider();
23
24             compilerparameters = new compilerparameters();
25             compilerparameters.referencedassemblies.add("system.dll");
26             compilerparameters.referencedassemblies.add("system.data.dll");
27             compilerparameters.referencedassemblies.add("system.deployment.dll");
28             compilerparameters.referencedassemblies.add("system.drawing.dll");
29             compilerparameters.referencedassemblies.add("system.windows.forms.dll");
30             compilerparameters.referencedassemblies.add("system.xml.dll");
31             compilerparameters.generateexecutable = false;
32             compilerparameters.generateinmemory = true;
33         }
34
35         ///
36         /// 获取代码的值
37         ///

38         /// csharpcode代码
39         /// 动态csharpcode代码的值
40         public static t getcodevalue(string csharpcode)
41         {
42             compilerresults compilerresults = csharpcodeprovider.compileassemblyfromsource(compilerparameters, createcode(csharpcode));
43
44             if (compilerresults.errors.haserrors)
45             {
46                 string errorstring = string.empty;
47                 errorstring += "编译错误:n";
48                 foreach (compilererror err in compilerresults.errors)
49                 {
50                     errorstring += err.errortext + "n";
51                 }
52                 throw new exception(errorstring);
53             }
54             else
55             {
56                 // 通过反射,调用动态类dynamicclass实例
57                 assembly assembly = compilerresults.compiledassembly;
58                 object dynamicclass = assembly.createinstance("dynamicnamespace.dynamicclass");
59                 methodinfo methodinfo = dynamicclass.gettype().getmethod("getvalue");
60
61                 return (t)methodinfo.invoke(dynamicclass, null);
62             }
63         }
64
65         ///
66         /// 创建代码
67         ///

68         /// csharpcode代码
69         /// 创建完成的代码
70         private static string createcode(string csharpcode)
71         {
72             stringbuilder strbuilder = new stringbuilder();
73             strbuilder.appendline("using system;");
74             strbuilder.appendline("using system.collections.generic;");
75             strbuilder.appendline("using system.componentmodel;");
76             strbuilder.appendline("using system.data;");
77             strbuilder.appendline("using system.drawing;");
78             strbuilder.appendline("using system.text;");
79             strbuilder.appendline("using system.windows.forms;");
80
81             strbuilder.appendline("namespace dynamicnamespace");
82             strbuilder.appendline("{");
83             strbuilder.appendline("      public class dynamicclass");
84             strbuilder.appendline("      {");
85             strbuilder.appendline("          public object getvalue()");
86             strbuilder.appendline("          {");
87             strbuilder.appendline("               return " + csharpcode + ";");
88             strbuilder.appendline("          }");
89             strbuilder.appendline("      }");
90             strbuilder.appendline("}");
91
92             string code = strbuilder.tostring();
93             return code;
94         }
95     }

相关文章

精彩推荐