好的,我确信这很简单,但我有一个高级时刻。
我有一个简单的BinaryExpression(greaterthan),左边是ParameterExpression,右边是ConstantExpression,我想将这个表达式编译成一个func,我可以调用并传递一个参数给...
var func = ...something with my exp....
bool result = func(myValue);
感谢哈桑,我根据自己的需要修改了答案......
var func = Expression.Lambda<Func<int,bool>>(myExpr, (ParameterExpression)myExpr.left).Compile();
答案 0 :(得分:7)
var param = Expression.Parameter(typeof(int));
var value = Expression.Constant(3);
var body = Expression.GreaterThan(param, value);
var checkValue = Expression.Lambda<Func<int, bool>>(body, param).Compile();
Console.WriteLine(checkValue(4));
Console.WriteLine(checkValue(2));