Asp.net C # obtain the value code of dynamic code

Source: Internet
Author: User

The following is a call (note the escape of special characters during the call)

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

The code is as follows:

1 usingmicrosoft. csharp;
2 usingsystem. reflection;
3 using system. codedom. compiler;
  4
5 public static class csharpcodevaluehelper <t>
6 {
7 /// <summary>
8 // compiler instance
9 /// </summary>
10 private static csharpcodeprovider {get; set ;}
11
12 /// <summary>
13 // compiler parameters
14 /// </summary>
15 private static compilerparameters {get; set ;}
16
17 /// <summary>
18 // Constructor
19 /// </summary>
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 /// <summary>
36 // get the code value
37 /// </summary>
38 // <param name = "csharpcode"> csharpcode </param>
39 // <returns> dynamic csharpcode value </returns>
40 public static t getcodevalue (string csharpcode)
41 {
42 compilerresults = csharpcodeprovider. compileassemblyfromsource (compilerparameters, createcode (csharpcode ));
43
44 if (compilerresults. errors. haserrors)
45 {
46 string errorstring = string. empty;
47 errorstring + = "compilation error: 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 // call a dynamic dynamicclass instance through reflection
57 assembly = compilerresults. compiledassembly;
58 object dynamicclass = assembly. createinstance ("dynamicnamespace. dynamicclass ");
59 methodinfo = dynamicclass. gettype (). getmethod ("getvalue ");
60
61 return (t) methodinfo. invoke (dynamicclass, null );
62}
63}
64
65 // <summary>
66 // create code
67 /// </summary>
68 // <param name = "csharpcode"> csharpcode </param>
69 // <returns> code after creation </returns>
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}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.