动态生成表达式以调用表达式.GreaterThan()



我正在尝试在运行时动态选择Expression中的方法。例如,我想实现类似于我下面尝试的东西:

ConstantExpression one = Expression.Constant(1);
ConstantExpression two = Expression.Constant(2);
// Here the 'GreaterThan' is the method name received at runtime:
var method = typeof(Expression).GetMethods().Single(mi => mi.Name == "GreaterThan" && mi.GetParameters().Length == 2);
var expr = Expression.Call(method, one, two);

在最后一行,我得到了错误:

System.ArgumentException: 'Expression of type 'System.Int32' cannot be used for parameter of type 'System.Linq.Expressions.Expression' of method 'System.Linq.Expressions.BinaryExpression GreaterThan(System.Linq.Expressions.Expression, System.Linq.Expressions.Expression)''

我想做的是通过在运行时动态选择Expression中的方法来构建lambda函数。在这里,方法名称指的是根据表达式方法与数字(或字符串(进行比较的某个方法。

什么是动态的?运算值(即"一"one_answers"二"的类型(?或者操作的类型("GreaterThan"、"LesThen"(?

如果是前者,你不需要做任何事情,因为表达式生成器会处理它

在中

Expression.GreaterThan(Expression.Constant(1), Expression.Constant(2));

Expression.GreaterThan(Expression.Constant("Some"), Expression.Constant("text"));

将自动选择int和字符串的适当大于运算符。

如果是后者,即动态选择操作,则需要编写

var expr = method.Invoke(null, new object[] { one, two });

这意味着您需要调用表达式方法来获得GreaterThan表达式,从而产生与编写Expression.GreaterThan(one, two)相同的结果。

对表达式方法调用Expression.Call类似于创建表达式来创建表达式。

我认为你应该避免在这里使用反射,而是使用开关来构建它。使用这种方法可以获得强类型的所有好处。看见https://www.codeproject.com/Articles/1079028/Build-Lambda-Expressions-Dynamically作为一个例子。

最新更新