C# 如何使用表达式来检查=针对用户定义的数据类型是否为null?
我不熟悉表达。我正在尝试这个,但它似乎不起作用C# 如何使用表达式来检查=针对用户定义的数据类型是否为null?,c#,lambda,expression-trees,C#,Lambda,Expression Trees,我不熟悉表达。我正在尝试这个,但它似乎不起作用 ParameterExpression pe = Expression.Parameter(typeof(Customer)); Expression left = Expression.Property(pe, "OrderList"); Expression right = Expression.Constant(null, typeof(Nullable)); Expression res = Expression.NotEqual(left
ParameterExpression pe = Expression.Parameter(typeof(Customer));
Expression left = Expression.Property(pe, "OrderList");
Expression right = Expression.Constant(null, typeof(Nullable));
Expression res = Expression.NotEqual(left, right);
我要签一个“无效手术例外”。在简单的If-Else语句中,如下所示
if(custObj.OrderList != null)
{...}
任何帮助都很好。只需使用
Expression right = Expression.Constant(null, left.Type);
它将为您提供与左操作数类型相同的
null
常量(本例中为属性)。问题在于您正在比较不同类型的对象。
您可以使用Expression.Constant(object value)
重载来解决此问题,
我假设属性OrderList
的类型是通过引用,如果不是,则无法进行此比较
ParameterExpression pe = Expression.Parameter(typeof(Customer));
Expression left = Expression.Property(pe, "OrderList");
Expression right = Expression.Constant(null);
Expression res = Expression.NotEqual(left, right);
OrderList
是否返回类型为Nullable
的对象?我怀疑这是真的。