using ExpressionVisitor how to dynamically get evaluate value of expression of a ParameterExpression

254 Views Asked by At

I have an expression that takes an expression as one of its parameters:

enter image description here

I am using a ExpressionVisitor to get the values of constants see this SO post.

but if a member is a memberAccess type I want to be able to evaluate what the actual value would be of that parameter. In this example, ($obj.Name).Length.

the example wants to check if an int (first value) is greater than the second value (in this case the expression). If it is a constant I know how to get the value of those, but I can't figure out how to evaluate the expression on the fly.

I have passed the whole object and the whole expression into the ExpressionVisitor so that they are available to be used to help if required.

internal class ValueExtractor : ExpressionVisitor
{
    private readonly object _item;
    private readonly MethodCallExpression _wholeExpression;

    public List<object> Arguments { get; }

    protected override Expression VisitMember(MemberExpression node)
    {
       if (memberType == ExpressionType.MemberAccess)
       {
           var exp = Expression.Lambda<Func<int>>(node, false, NotSureAboutThis);
           exp.Compile().Invoke(NotSureWhichPropGoesHere);
       }

    }
}

The node value in VisitMember is:

enter image description here

I have access to T and TMember. where T is the main object this is all being called (has property Age) and TMember being int in this case (Age).

I am not sure if the example above is going in the right direction, if it is I am not sure how to finish it up.

0

There are 0 best solutions below