NetCore DynamicExpresso 动态表达式使用例子

复制代码
Simple.cs 简单使用例子
复制代码
using DynamicExpresso;

namespace DynamicExpressoDemo
{
    class Customer { public string Name { get; set; } }

    public class Simple
    {
        public static void Test()
        {
            // 返回结果
            Interpreter target1 = new Interpreter();
            double result1 = target1.Eval<double>("Math.Min(x, y) + 5", new Parameter("x", typeof(double), 1), new Parameter("y", typeof(double), 2));
            Console.WriteLine($"result:{result1}");// 输出:6

            // 定义变量并赋值
            var target2 = new Interpreter().SetVariable("myVar", 23);
            var result2 = target2.Eval("myVar + 10");
            Console.WriteLine($"result:{result2}");// 输出:33

            // 动态参数
            var target3 = new Interpreter();
            var parameters = new[] { new Parameter("x", 23), new Parameter("y", 7) };
            var result3 = target3.Eval("x + y", parameters);
            Console.WriteLine($"result:{result3}");// 输出:30

            var target = new Interpreter();
            target.SetVariable("a", new Customer { Name = "AA" });
            target.SetVariable("b", new Customer { Name = "BB" });
            Console.WriteLine(target.Eval("a.Name"));// 输出:AA
            Console.WriteLine(target.Eval("b.Name")); // 输出:BB
        }
    }
}

QueryMemoryData.cs 查询内存数据例子

复制代码
using DynamicExpresso;

namespace DynamicExpressoDemo
{
    class Trade
    {
        public int TradeID { get; set; }

        public string TradeTitle { get; set; }

        public DateTime Created { get; set; }

        public int CustomerID { get; set; }
    }

    public class QueryMemoryData
    {
        static Interpreter interpreter = new Interpreter();

        public static void Filter()
        {
            var tradeList = new List<Trade>()
            {
                new Trade() { TradeID = 1, TradeTitle = "交易1", Created = Convert.ToDateTime("2020/8/1"), CustomerID = 1 },
                new Trade() { TradeID = 2, TradeTitle = "交易2", Created = Convert.ToDateTime("2020/8/5"), CustomerID = 2 },
                new Trade() { TradeID = 3, TradeTitle = "交易3", Created = Convert.ToDateTime("2020/8/10"), CustomerID = 3 }
            };

            // 查询内存值
            string whereExpression = "(trade.CustomerID == 1 || trade.CustomerID == 2 || trade.CustomerID == 10) && trade.Created <= Convert.ToDateTime(\"2020-08-20\")"; 
            Func<Trade, bool> queryFunc = interpreter.ParseAsDelegate<Func<Trade, bool>>(whereExpression, "trade"); 
            var list = tradeList.Where(queryFunc).ToList();// list + DynamicExpresso

            foreach (var a in list)
            {
                a.TradeTitle = a.TradeTitle + "_update";// 更新内存值
                Console.WriteLine(a.ToString());
            }
        }
    }
}
复制代码
相关推荐
啊啊啊六子4 小时前
windows下安装wsl的ubuntu,同时配置深度学习环境
windows·深度学习·ubuntu
码农阿豪6 小时前
Windows系统电脑安装TightVNC服务端结合内网穿透实现异地远程桌面
windows
希忘auto10 小时前
详解登录MySQL时出现SSL connection error: unknown error number错误
windows·mysql
paj12345678911 小时前
JDK1.8 Stream流使用
java·windows
狐凄13 小时前
Python一些项目bug
windows·python·bug
songyuc13 小时前
【测试】对比 ctypes vs pywin32 在捕获键鼠事件上的灵敏度和响应速度
windows
樱花DR13 小时前
mfc100u.dll 文件损坏问题:一步步解决法
windows·经验分享
purples.14 小时前
上传本地文件到GitHub
windows·git·github
raoxiaoya14 小时前
使用windows窗口展示go-echarts图表
windows·golang·echarts
下面我就简单说两句16 小时前
物联网无线局域网WiFi开发(二):WiFi_RTOS_SDK
windows·物联网