评估TypeAs表达式

时间:2012-05-31 12:39:12

标签: c# .net lambda expression evaluate

我正在尝试评估表达式以检查评估的输出是否为空。 我的代码如下所示:

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;
    }
}

这是调用方法的方法,用于解释“项目”的来源:

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

我尝试调用Expression.Lambda(expr,some_parameters).Compile()来获取一个委托,我可以使用它来获取'as'的输出,但我不断收到'变量'项'类型'的错误X'从范围''引用,但未定义'。

我猜我没有给Lambda方法提供正确的参数,有人可以帮忙知道正确的Lambda调用应该是什么样子以及我应该为它提供什么参数?

0 个答案:

没有答案