提问人:Roberto Vanoli 提问时间:11/15/2023 更新时间:11/15/2023 访问量:28
使用 eval-expression.net 来计算字典中的项目,就好像它们是对象的属性一样
using eval-expression.net to evaluate items of a dictionary, as if they were properties of an object
问:
我正在尝试 eval-expression.net,我想评估像“One.Id==1”而不是“customers[”One“]这样的表达式。Id==1“('One' 实际上是 Dictionary<string, Customer> 中的键)
请看下面的例子:我的想法是使用别名来指示库将“One”替换为 customers[“One”],但这会导致 EvalException:“找不到类型。
有没有人对如何达到预期的结果有任何建议?
[TestMethod()]
public void Register1()
{
Dictionary<string, Customer> customers = new()
{
{ "One", new Customer() { Id = 1 } },
{ "Two", new Customer() { Id = 2 } }
};
var evalContext = new EvalContext();
var r1 = evalContext.Execute<bool>("customers[\"One\"].Id==1", new { customers });
foreach (var i in customers)
{
evalContext.RegisterAlias(i.Key, $"customers[\"{i.Key}\"]");
}
var r2 = evalContext.Execute<bool>("One.Id==1", new { customers }); //EvalException
}
class Customer
{
public int Id { get; set; }
}
答: 暂无答案
评论