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?