I need to use ExpressionVisitor to analyse an Expression before executing it. For my needs, i need to evaluate the right part of a Divide expression but i don't know how to do it. Here's a sample code that i have:
internal class RulesChecker : ExpressionVisitor
{
private readonly object data;
public RulesChecker(object data)
{
this.data = data;
}
protected override Expression VisitBinary(BinaryExpression node)
{
if (node.NodeType == ExpressionType.Divide)
{
var rightExpression = node.Right;
// compile the right expression and get his value
}
return base.VisitBinary(node);
}
}
Suppose that i have this code to evaluate:
Expression<Func<DataInfo, decimal?>> expression = x => x.A / (x.B + x.C);
var rulesChecker = new RulesChecker(data);
rulesChecker.Visit(expression);
In the VisitBinary function, i will receive a node that will contain the left and right part of the divide operation. My question is, how can i evaluate the value that i will get in the right part of the operation?
Usually you could use this method to evaluate a lambda expression (and pass ):
However, in your case this won't work, because the expression you're trying to evaluate depends on
x, which cannot be evaluated, unless you specify a concrete value for it, as Wiktor suggested.In order to specify a value for the parameter, you need to modify the method as such:
This version of the method, however, must take as a parameter the ExpressionParameter object that represents the
xin your expression in order for it to know what to do with the value passed toDynamicInvoke().In order to obtain the appropriate
ExpressionParameterobject you need access to the root expression, not to one of its nodes, so I guess it would be awkward to do it in a visitor.