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()); } } } }
标签:DynamicExpresso,Console,NetCore,CustomerID,Interpreter,var,new,public,表达式 From: https://www.cnblogs.com/chen1880/p/18411774