Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

calculate result from a string expression dynamically

Tags:

c#

Is there a way to calculate the result of a string expression, for example mystring = "2*a+32-Math.Sin(6)" dynamically knowing that a is a variable that I have, may be there is some dynamic solution or using System.Reflection

string mystring = "2*a+32-Math.Sin(6)"`;
decimal result = SomeMethod(mystring,3); // where a = 3 for example
like image 345
S3ddi9 Avatar asked Sep 14 '12 20:09

S3ddi9


3 Answers

How about making javascript calculate your expression?

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)");
like image 50
L.B Avatar answered Nov 11 '22 22:11

L.B


if found this its working perfectly, sorry for the bother, all I wanted is on the fly compiler. & this what i get

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;

    }
like image 44
S3ddi9 Avatar answered Nov 11 '22 23:11

S3ddi9


You can use Compute methood of Datatable in SomeMethod in following way:

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

Simply, you can call like this:

        result = Somemethod(3);
like image 33
Akash KC Avatar answered Nov 11 '22 23:11

Akash KC