I've solved it, here are the details of the resolved class:

Source: Internet
Author: User
Tags foreach bool eval expression tostring
If you enter a simple mathematical formula in a text box: A formula like 200+100*290, how to calculate their value:

Using System;
Using System.CodeDom;
Using System.CodeDom.Compiler;
Using Microsoft.csharp;
Using System.Text;
Using System.Reflection;

Namespace Adoguy
{
<summary>
Summary description for Class1.
</summary>
public class evaluator
{

Public evaluator (evaluatoritem[] items)
{
Constructevaluator (items);
}

Public Evaluator (Type returntype, string expression, string name)
{
evaluatoritem[] items = {New Evaluatoritem (returntype, expression, name)};
Constructevaluator (items);
}

Public evaluator (Evaluatoritem item)
{
evaluatoritem[] items = {Item};
Constructevaluator (items);
}

private void Constructevaluator (evaluatoritem[] items)
{
ICodeCompiler comp = (new CSharpCodeProvider (). CreateCompiler ());
CompilerParameters cp = new CompilerParameters ();
Cp. Referencedassemblies.add ("System.dll");
Cp. Referencedassemblies.add ("System.Data.dll");
Cp. Referencedassemblies.add ("System.Xml.dll");
Cp. GenerateExecutable = false;
Cp. GenerateInMemory = true;

StringBuilder code = new StringBuilder ();
Code. Append ("Using System; \ n ");
Code. Append ("Using System.Data; \ n ");
Code. Append ("Using System.Data.SqlClient; \ n ");
Code. Append ("Using System.Data.OleDb; \ n ");
Code. Append ("Using System.Xml; \ n ");
Code. Append ("namespace Adoguy {\ n");
Code. Append ("public class _evaluator {\ n");
foreach (Evaluatoritem item in items)
{
Code. AppendFormat ("public {0} {1} ()"),
Item. Returntype.name,
Item. Name);
Code. Append ("{");
Code. AppendFormat ("Return ({0});", item. Expression);
Code. Append ("}\n");
}
Code. Append ("}}");

CompilerResults CR = Comp.compileassemblyfromsource (CP, code.) ToString ());
if (CR. Errors.haserrors)
{
StringBuilder error = new StringBuilder ();
Error. Append ("Error compiling Expression:");
foreach (CompilerError err in CR. Errors)
{
Error. AppendFormat ("{0}\n", err.) ErrorText);
}
throw new Exception ("Error compiling Expression:" + error. ToString ());
}
Assembly a = cr.compiledassembly;
_compiled = A.createinstance ("Adoguy._evaluator");
}

public int Evaluateint (string name)
{
return (int) Evaluate (name);
}

public string evaluatestring (string name)
{
Return (String) Evaluate (name);
}

public bool Evaluatebool (string name)
{
return (BOOL) Evaluate (name);
}

public Object Evaluate (string name)
{
MethodInfo mi = _compiled.gettype (). GetMethod (name);
Return MI. Invoke (_compiled, NULL);
}

static public int Evaluatetointeger (string code)
{
Evaluator eval = new Evaluator (typeof (int), code, staticmethodname);
return (int) eval. Evaluate (Staticmethodname);
}

static public string evaluatetostring (string code)
{
Evaluator eval = new Evaluator (typeof (String), code, staticmethodname);
Return (string) eval. Evaluate (Staticmethodname);
}

static public bool Evaluatetobool (string code)
{
Evaluator eval = new Evaluator (typeof (BOOL), code, staticmethodname);
return (BOOL) eval. Evaluate (Staticmethodname);
}

Static public Object Evaluatetoobject (string code)
{
Evaluator eval = new Evaluator (typeof (object), code, staticmethodname);
Return eval. Evaluate (Staticmethodname);
}

Const string staticmethodname = "__foo";
Type _compiledtype = null;
Object _compiled = null;

}//class

public class Evaluatoritem
{
Public Evaluatoritem (Type returntype, string expression, string name)
{
ReturnType = ReturnType;
Expression = Expression;
name = name;
}

Public Type ReturnType;
public string Name;
public string Expression;
}//class
}//namespace





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.