Dynamic Expresso

  • Available platforms: .NET Core 2.0, .NET 4.5, .NET 4.6.1
  1. Dynamic Expresso is an interpreter for simple C# statements written in .NET Standard 2.0. Dynamic Expresso embeds its own parsing logic, really interprets C# statements by converting it to .NET lambda expressions or delegates.
  2. Using Dynamic Expresso developers can create scriptable applications, execute .NET code without compilation or create dynamic linq statements.
  3. Statements are written using a subset of C# language specifications. Global variables or parameters can be injected and used inside expressions. It doesn't generate assembly but it creates an expression tree on the fly.

Git地址

https://github.com/davideicardi/DynamicExpresso

public class Program
    {
        private static Interpreter _target;
        static void Main(string[] args)
        {
            _target = new Interpreter();

            //返回指定數(shù)字的指定次冪
            Func<double, double, double> pow = (x, y) => Math.Pow(x, y);
            //開平方根
            Func<double, double> sqrt = (x) => Math.Sqrt(x);
            //最大值
            Func<double, double, double> max = (x, y) => Math.Max(x, y);
            //最小指
            Func<double, double, double> min = (x, y) => Math.Min(x, y);

            _target.SetFunction("pow", pow);
            _target.SetFunction("sqrt", sqrt);
            _target.SetFunction("max", max);
            _target.SetFunction("min", min);

            Console.WriteLine(_target.Eval("pow(3, 2)"));
            Console.WriteLine(_target.Eval("sqrt(9)"));
            Console.WriteLine(_target.Eval("max(3, 2)"));
            Console.WriteLine(_target.Eval("min(3, 2)"));

            //打印普通數(shù)學(xué)表達(dá)式計(jì)算的值
            string expression = "10/2+5+(10+5)*6-40";
            Console.WriteLine(_target.Eval(expression));
        }
    }

測(cè)試結(jié)果如下:


測(cè)試結(jié)果.png
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

友情鏈接更多精彩內(nèi)容