1

I'm trying to evaluate an expression to check if the output of the evaluation is null. My code looks like this:

public class ObjectHelper<TType>
{
    public static bool PathHasNull(object pParentObjectInstance, Expression<Func<TType, object>> pPathToValue)
    {
        Expression lExpressionPart = pPathToValue.Body;
        if (lExpressionPart.NodeType == ExpressionType.TypeAs)
            {
                ExpressionParameter[] some_params;
                //some code that creates the ExpressionParameters goes here

                Delegate lDelegate = Expression.Lambda(lExpressionPart, some_params).Compile();
                lMemberValue = lDelegate.DynamicInvoke(lInstance); //not sure about this...
            }
            if (lMemberValue == null)
            {
                return true;
            }

        return false;
    }
}

This is how the method gets called, to explain where the 'item' is from:

TypeX x = GetXInstance();
ObjectHelper<TypeX>.PathHasNull(x, (item => item.Foo.Bar.Something as SomeTypeOfThing).SomeProperty);

I tried calling Expression.Lambda(expr, some_parameters).Compile() to get a delegate I can use to then get the output of the 'as' but I keep getting an error that 'variable 'item' of type 'X' referenced from scope '', but it is not defined'.

I'm guessing I'm not giving the correct parameters to the Lambda method, could someone help with an idea on what the correct Lambda call should look like and what parameters I should provide for it?

  • 2
    Rather than "something like" - could you give us a short but complete example which demonstrates the problem? – Jon Skeet May 31 '12 at 12:42
  • I edited my post and included some code for the method where I need to do the evaluation, I removed some irrelevant code bits from the method so I don't post miles of code... – user1428346 May 31 '12 at 13:00

0 Answers0