lundi 19 novembre 2018

Complex Expression Tree with no changing context of param

i need to dinamically generate expression like this:

Expression<Func<MyClass, bool>> expr = x => (x.SomeField.CompareTo(someValue) <= 0);

trying to do it like this:

var paramExpr = Expression.Parameter(typeof(MyClass), "x");
Expression<Func<MyClass, FieldType>> pathToField = x => x.SomeField;
Expression path = pathToField;
if (!(path is LambdaExpression lambdaMember))
    throw ...;
Expression valueExpr = Expression.Constant(someValue);
var bodyExpr = Expression.LessThanOrEqual(Expression.Call(lambdaMember.Body, "CompareTo", null, valueExpr ), Expression.Constant(0))},
return Expression.Lambda<Func<MyClass, FieldType>>(bodyExpr, paramExpr);

but always getting error when trying to compile this:

variable 'x' of type 'MyClass' referenced from scope '', but it is not defined

how i could do this correctly?





Aucun commentaire:

Enregistrer un commentaire