How to calculate the value of a string directly in C # like JS

Source: Internet
Author: User
Tags eval expression reflection tostring
js| string today in the forum to find a problem is more typical, we use JS can directly use the eval to get an expression of the value, in C # can it? The answer is yes, it is achievable in the rich class libraries provided by. NET, but without JS, I'll use a simple example to illustrate the value of a string expression using the compiler and reflection. We create a Windows Form that has the following methods:

Reference namespaces:

Using System;

Using System.Text;

Using System.CodeDom.Compiler;

Using System.Reflection;

Using System.IO;

Using Microsoft.csharp;

The specific meaning of the namespace does not say, the following look at the code, in order to let our project can be reused we create a new class Library project name: Coustomeval One of the classes is the value used to compute a broken string. The detailed code looks like this:

Namespace coustomeval{

///

Summary description for Class1.

///

public class myeval{

Public Myeval () {

//

Todo:add constructor Logic here

//

}

public Object Eval (string ccharpcode) {

CSharpCodeProvider CSharpCodeProvider = new CSharpCodeProvider ();

ICodeCompiler compiler = Csharpcodeprovider.createcompiler ();

CompilerParameters cp = new CompilerParameters ();

Cp. Referencedassemblies.add ("System.dll");

Cp.compileroptions = "/t:library";

Cp. GenerateInMemory = true;

StringBuilder mycode = new StringBuilder ();

Mycode.append ("Using System;");

Mycode.append ("namespace coustomeval{");

Mycode.append ("class Mylib {private" +ccharpcode+ "public int MyMethod () {return i;}}");

Mycode.append ("}");

CompilerResults cr = Compiler.compileassemblyfromsource (cp,mycode.tostring ());

Assembly Assembly = cr.compiledassembly;

Object TMP = assembly. CreateInstance ("Coustomeval.mylib");

Type type = tmp. GetType ();

MethodInfo mi = type. GetMethod ("MyMethod");

Object result = mi. Invoke (Tmp,null);

return result;

}

}

}



The above class library only has the general representative does not have the general usability, the important is provides a method to implement it, therefore I use in the inside the hard coding way, if you like can use other way, including reads the file, reads the database to obtain the code paragraph and so on. And then we're building a test project that has a test form with a button and two text boxes, one text box to enter the value to calculate, and the other to display the results of the calculation, detailed code as follows:

private void Button1_Click (object sender, System.EventArgs e) {

Myeval eval = new Myeval ();

Object result = eval. Eval (This.textBox1.Text);

This.textBox2.Text = result. ToString ();

}

In this project, we need to refer to the above project, so that we can use the Myeval class to achieve the calculation of the value of the TextBox1, and finally can display the value in the TextBox2, the results of the operation of the code here I do not post. Only give my test results, because the problem originated from the forum so I am using the original test requirements in the forum.

Test Result: Enter int i = 10 in textbox, and click Button1 will show 10 in TextBox2.


Related Article

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.