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());
            }
        }
    }
}
复制代码
相关推荐
tadus_zeng10 小时前
Windows C++ 排查死锁
c++·windows
EverestVIP10 小时前
VS中动态库(外部库)导出与使用
开发语言·c++·windows
抛物线.14 小时前
inhibitor_tool
windows
宋冠巡15 小时前
Windows安装Docker(Docker Desktop)
windows·docker·容器
淬渊阁16 小时前
windows技术基础知识
windows
niandb1 天前
The Rust Programming Language 学习 (九)
windows·rust
virelin_Y.lin1 天前
系统与网络安全------Windows系统安全(1)
windows·安全·web安全·系统安全
电星托马斯1 天前
C++中顺序容器vector、list和deque的使用方法
linux·c语言·c++·windows·笔记·学习·程序人生
麻芝汤圆1 天前
使用 MapReduce 进行高效数据清洗:从理论到实践
大数据·linux·服务器·网络·数据库·windows·mapreduce
@郭小茶1 天前
windows部署docker
windows·docker·容器