Compile an expression that requires a parameter

Well, I'm sure it is simple, but I have a senior point.

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

var func = ...something with my exp.... bool result = func(myValue); 

Thanks to Hassan, I changed his response to my needs ...

 var func = Expression.Lambda<Func<int,bool>>(myExpr, (ParameterExpression)myExpr.left).Compile(); 
+4
source share
1 answer
 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)); 
+7
source

All Articles