Compile expression that requires a parameter

橙三吉。 提交于 2019-12-22 08:18:57

问题


Ok, I am sure this is simple, but I am having a senior moment.

I have a simple BinaryExpression (greaterthan) the left side is a ParameterExpression and the right side is a ConstantExpression I want to compile this expression to a func that I can call and pass a parameter to...

var func = ...something with my exp....

bool result = func(myValue);

Thanks to Hasan, I modified his answer to my needs...

var func = Expression.Lambda<Func<int,bool>>(myExpr, (ParameterExpression)myExpr.left).Compile();

回答1:


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));


来源:https://stackoverflow.com/questions/7708895/compile-expression-that-requires-a-parameter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!