• NetCore中使用DynamicExpresso、Z.Expressions、LambdaParser进行安字符串求值


    例子如下:

    Z.Expressions从2.0开始支持了NetCore,但是收费的。其它两者免费。建议使用DynamicExpresso,免费而且速度快。LambdaParser目前支持太少。

    static void Main(string[] args)
            {
    
                Console.OutputEncoding = Encoding.Unicode;
    
                System.Diagnostics.Stopwatch sp = new Stopwatch();
                sp.Start();
                var code = "i<100&&s.Contains("测试")";
                var interpreter = new DynamicExpresso.Interpreter();
                var result = interpreter.ParseAsDelegate<Func<int, string, bool>>(code, "i", "s");
                //var result = interpreter.ParseAsExpression<Func<int, string, bool>>(code, "i", "s").Compile();
                for (int i = 0; i < 18000000; i++)
                {
                    var v = result(i, "我在测试" + i);//2213ms
                    //Console.WriteLine(v);
                }
                sp.Stop();
                Console.WriteLine("time:" + sp.ElapsedMilliseconds + "ms");
    
                //System.Diagnostics.Stopwatch sp = new Stopwatch();
                //sp.Start();
                ////var code = @"
                ////     var list = new List<int>() { 1, 2, 3, 4, x };
                ////        var filter = list.Where(x => x < 4);
                ////        return filter.Sum(x => x); ";
                //var code = "i<100&&s.Contains("测试")";
                ////var result = Z.Expressions.Eval.Compile<Func<int, string, bool>>(code, "i", "s");
    
                //for (int i = 0; i < 100000; i++)
                //{
                //    var v = result(i, "我爱测试");
                //    //Console.WriteLine(v);
                //}
                //sp.Stop();
                //Console.WriteLine("time:" + sp.ElapsedMilliseconds + "ms");
    
                //var parse = new LambdaParser();
                //while (true)
                //{
                //    try
                //    {
                //        Console.WriteLine(new string('_', 20));
                //        var dics = new Dictionary<string, object>();
                //        dics.Add("x1", 32.0);
                //        dics.Add("x2", 23.0);
                //        dics.Add("x3", 34.5);
                //        dics.Add("x4", 34.5);
                //        dics.Add("s1", "my name is xxxxzhu!");
                //        dics.Add("b1", true);
                //        foreach (var item in dics)
                //        {
                //            Console.WriteLine(item.Key + "	" + item.Value);
                //        }
                //        Console.WriteLine(new string('_', 20));
                //        Console.WriteLine("expr:");
                //        var expr = Console.ReadLine();
                //        var value = parse.Eval(expr, dics);
                //        Console.WriteLine("value======================" + value);
                //    }
                //    catch (Exception ex) { Console.WriteLine("error====================:" + ex.Message); }
                //}
            }
  • 相关阅读:
    mac安装搜狗
    idea的阿里代码规范检查
    记录windows10闪屏
    github访问不了
    线程安全问题例子
    简单负载均衡工具类
    git将远程分支回归到指定版本
    minio的使用
    php禁止浏览器使用缓存页面的方法
    百度seo
  • 原文地址:https://www.cnblogs.com/songxingzhu/p/6737618.html
Copyright © 2020-2023  润新知