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

xiaoxiao2021-03-06  92

Today, I'm looking for a problem in the forum. We can use the EVAL to get the value of an expression in C # when using JS. The answer is affirmative, in the rich class library provided by .NET, this is achievable, but there is no JS so simply below I will use a simple example to illustrate the value of a string expression using the compiler and reflection. We have the following ways to build a Windows form:

Quote Namespace:

Using system;

Using system.text;

Using system.codedom.compiler;

Using system.reflection;

Using system.io;

USING Microsoft.csharp;

The specific meaning of the name is not to say, look at the code below, in order to allow our project to reuse our newly built type library project name: coustomeval, one class is used to calculate a value of a string. The detailed code is as follows:

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 () {returni}}

mycode.append ("}");

CompilerResults cr = compiler.compileassemblyfromsource (cp, mycode.toString ());

AskEMBLY 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 general representative without general use, and it is important to provide a way to implement it, so I use hard-coded ways inside, if you like other ways, including reading files, Read the database acquisition code segment, and the like. Then we are building a test engineering, there is a test form on which there is a button and two text boxes, one of which is used to enter the value to be calculated, another display calculation result, The detailed code is as follows: Private void button1_click (object sender, system.eventargs e) {

Myeval eval = new myeval ();

Object results = evult = evult (this.textbox1.text);

THIS.TEXTBOX2.TEXT = Result.toString ();

}

In this project, you need to reference the project above so that we can use the Myeval class to implement the value of the TEXTBOX1, and finally display the value inside the textbox2. The result of the code runs here. Only give my test results, because the problem is from the forum, I am using the original test requirements in the forum.

Test results: Enter int i = 10 in TextBox; you will display 10 in TextBox2 after clicking Button1.

Of course you can use more complex computational expressions! The purpose here is to give you a practice of implementing this feature, as for the complex degree of readers to be determined by themselves.

Thank you for reading, there is any mistakes and omissions in the text, please correct it, you can also give me a letter wu_jian830@hotmail.com.

转载请注明原文地址:https://www.9cbs.com/read-122384.html

New Post(0)