Is there an eval function In C#?

前端 未结 7 1914
[愿得一人]
[愿得一人] 2020-12-05 03:41

I\'m typing an equation into a TextBox that will generate the graph of the given parabola. Is it possible to use an eval function? I\'m using C# 2010 and it doesn\'t have Mi

7条回答
  •  执笔经年
    2020-12-05 04:00

    I created the ExpressionEvaluatorCs Nuget package inspired by mar.k's answer and also added the option to specify return type.

    Sample code

    // Returns object 6
    object result1 = ExpressionEvaluator.Evaluate("(1 + 2) * 2");
    
    // Returns int 6
    int result1 = ExpressionEvaluator.Evaluate("(1 + 2) * 2");
    
    

提交回复
热议问题