Reflection application-compile a string to obtain the computing result

Source: Internet
Author: User
1 using System;
2 using System. Data;
3 using System. Configuration;
4 using System. Collections;
5 using System. Web;
6 using System. Web. Security;
7 using System. Web. UI;
8 using System. Web. UI. WebControls;
9 using System. Web. UI. WebControls. WebParts;
10 using System. Web. UI. HtmlControls;
11 using System. Text;
12 using System. CodeDom. Compiler;
13 using Microsoft. CSharp;
14 using System. Reflection;
15
16 public partial class Default2: System. Web. UI. Page
17 {
18 protected void Page_Load (object sender, EventArgs e)
19 {
20 TimeSpan Ts = new TimeSpan ();
21 DateTime dtStar = DateTime. Now;
22 Evaluator ev = new Evaluator ();
23 Response. Write (ev. Eval ("2*2*3*10*3.12/811 "));
24 DateTime dtEnd = DateTime. Now;
25 Ts = dtEnd-dtStar;
26 Response. Write ("<br>" + Ts. TotalMilliseconds. ToString ());
27}
28}
29 public class Evaluator
30 {
31 private static CodeDomProvider comp = new CSharpCodeProvider ();
32 private static CompilerParameters cp = new CompilerParameters ();
33 private static MethodInfo mi;
34 public object Eval (string expression)
35 {
36 StringBuilder codeBuilder = new StringBuilder ();
37 codeBuilder. Append ("using System ;");
38 codeBuilder. Append ("using Microsoft. CSharp ;");
39 codeBuilder. Append ("");
40 codeBuilder. Append ("public class Mode ");
41 codeBuilder. Append ("{");
42 codeBuilder. Append ("public static object Func ()");
43 codeBuilder. Append ("{");
44 codeBuilder. Append ("return" + expression + ";");
45 codeBuilder. Append ("}");
46 codeBuilder. Append ("}");
47 cp. ReferencedAssemblies. Add ("System. dll ");
48 cp. GenerateExecutable = false;
49 cp. GenerateInMemory = true;
50 string code = codeBuilder. ToString ();
51 string [] stra = new string [] {code };
52 CompilerResults cr = comp. CreateCompiler (). CompileAssemblyFromSource (cp, code );
53 if (cr. Errors. HasErrors)
54 {
55 return null;
56}
57 else
58 {
59 Assembly a = cr. CompiledAssembly;
60 Type t = a. GetType ("Mode ");
61 mi = t. GetMethod ("Func", BindingFlags. Static | BindingFlags. Public );
62 return mi. Invoke (null, new object [0]);
63}
64}
65}

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.