动态计算字符串表达式的结果

时间:2012-09-14 20:07:22

标签: c#

例如,有没有办法计算字符串表达式的结果 mystring = "2*a+32-Math.Sin(6)"动态地知道 a 是我拥有的变量,可能有一些动态解决方案或使用System.Reflection

string mystring = "2*a+32-Math.Sin(6)"`;
decimal result = SomeMethod(mystring,3); // where a = 3 for example

6 个答案:

答案 0 :(得分:17)

如何让javascript计算你的表达式?

Type scriptType = Type.GetTypeFromCLSID(Guid.Parse("0E59F1D5-1FBE-11D0-8FF2-00A0D10038BC"));

dynamic obj = Activator.CreateInstance(scriptType, false);
obj.Language = "javascript";

var res = obj.Eval("a=3; 2*a+32-Math.sin(6)");

答案 1 :(得分:4)

您可以通过以下方式在SomeMethod中使用数据表的Compute方法:

       static decimal Somemethod(int val)
        {
            var result = (decimal)new DataTable().Compute(string.Format("2*{0}+32-{1}", val, Math.Sin(6)), "");
            return result;
        }

简单地说,您可以这样打电话:

        result = Somemethod(3);

答案 2 :(得分:3)

如果发现它的工作完美,抱歉打扰,我想要的就是动态编译器。 &安培;这就是我得到的

MessageBox.Show(Eval("5*3-Math.Sin(12) + 25*Math.Pow(3,2)").ToString());

public static object Eval(string sCSCode)
    {

        CodeDomProvider icc = CodeDomProvider.CreateProvider("C#");
        CompilerParameters cp = new CompilerParameters();

        cp.ReferencedAssemblies.Add("system.dll");
        cp.ReferencedAssemblies.Add("system.xml.dll");
        cp.ReferencedAssemblies.Add("system.data.dll");
        cp.ReferencedAssemblies.Add("system.windows.forms.dll");
        cp.ReferencedAssemblies.Add("system.drawing.dll");

        cp.CompilerOptions = "/t:library";
        cp.GenerateInMemory = true;

        StringBuilder sb = new StringBuilder("");
        sb.Append("using System;\n");
        sb.Append("using System.Xml;\n");
        sb.Append("using System.Data;\n");
        sb.Append("using System.Data.SqlClient;\n");
        sb.Append("using System.Windows.Forms;\n");
        sb.Append("using System.Drawing;\n");

        sb.Append("namespace CSCodeEvaler{ \n");
        sb.Append("public class CSCodeEvaler{ \n");
        sb.Append("public object EvalCode(){\n");
        sb.Append("return " + sCSCode + "; \n");
        sb.Append("} \n");
        sb.Append("} \n");
        sb.Append("}\n");

        CompilerResults cr = icc.CompileAssemblyFromSource(cp, sb.ToString());
        if (cr.Errors.Count > 0)
        {
            MessageBox.Show("ERROR: " + cr.Errors[0].ErrorText,
               "Error evaluating cs code", MessageBoxButton.OK,
               MessageBoxImage.Error);
            return null;
        }

        System.Reflection.Assembly a = cr.CompiledAssembly;
        object o = a.CreateInstance("CSCodeEvaler.CSCodeEvaler");

        Type t = o.GetType();
        System.Reflection.MethodInfo mi = t.GetMethod("EvalCode");

        object s = mi.Invoke(o, null);
        return s;

    }

答案 3 :(得分:2)

我认为没有任何原生支持你所要求的,但是有很多方法可以实现它,但这些方法都不是完全无关紧要的,而是按照我认为它们很简单的顺序:

答案 4 :(得分:1)

创建抽象类使用要用于计算表达式的方法。在运行时创建一个继承该类的新类。我很快就会分享这些代码。

这里承诺的是执行此操作的代码:

    public class BaseClass
    {
    public BaseClass(){}
    public virtual double Eval(double x,double y){return 0;}
}
public class MathExpressionParser
{
    private BaseClass Evalulator=null;
    public MathExpressionParser(){}
    public bool Intialize(string equation)
    {
        Microsoft.CSharp.CSharpCodeProvider cp=new Microsoft.CSharp.CSharpCodeProvider();
        System.CodeDom.Compiler.ICodeCompiler comp=cp.CreateCompiler();
        System.CodeDom.Compiler.CompilerParameters cpar=new CompilerParameters();

        cpar.GenerateInMemory=true;
        cpar.GenerateExecutable=false;
        cpar.ReferencedAssemblies.Add("system.dll");
        cpar.ReferencedAssemblies.Add("EquationsParser.exe");   //Did you see this before;

        string sourceCode="using System;"+
                          "class DrivedEval:EquationsParser.BaseClass" +
                          "{"+   
                                  "public DrivedEval(){}"+
                                  "public override double Eval(double x,double y)"+
                                  "{"+
                                        "return "+ /*Looook here code insertion*/ equation +";"+
                                  "}"+
                          "}";
        //the previouse source code will be compiled now(run time);
        CompilerResults result=comp.CompileAssemblyFromSource(cpar,sourceCode);

        //If there are error in the code display it for the programmer who enter the equation
        string errors="";
        foreach(CompilerError rrr in result.Errors)
        {
            if(rrr.IsWarning)
                continue;
            errors+="\n"+rrr.ErrorText;
            errors+="\n"+rrr.ToString();
        }
        //You Can show error if there in the sourceCode you just compiled uncomment the following
        //MessageBox.Show(errors);
        if(result.Errors.Count==0&&result.CompiledAssembly!=null)
        {
            Type objtype=result.CompiledAssembly.GetType("DrivedEval");
            try
            {
                if(objtype!=null)
                {
                    Evalulator=(BaseClass)Activator.CreateInstance(objtype);
                }
            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.Message,"Error in Creation the object");
            }
            return true;
        }
        else return false;
    }
    public double evaluate(double x,double y)
    {
        if(Evalulator==null)
            return 0.0;
        return this.Evalulator.Eval(x,y);
    }
}

您将需要执行以下简单测试以确保其有效:

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

    if(parser.Intialize(textBox1.Text)==false)
{
    MessageBox.Show("Check equation");
    return;
}
textBox4.Text=(parser.evaluate(double.Parse(textBox2.Text),double.Parse(textBox3.Text))).ToString();
}

答案 5 :(得分:1)

以下是我了解的一些最流行的方法,这些方法可以在C#中动态评估字符串表达式。

Microsoft解决方案

非Microsoft解决方案(不是说这有什么问题)