我是表达式的新手。我正在尝试这个,但它似乎不起作用。
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);
我遇到了“InvalidOperationException”。在简单的 If-Else 语句中是这样的
if(custObj.OrderList != null)
{...}
任何帮助都会很棒。
最佳答案
简单使用
Expression right = Expression.Constant(null, left.Type);
它将为您提供与左操作数(在您的情况下为属性)类型相同的 null
常量。
关于c# - 如何使用表达式检查 !=null 针对用户定义的数据类型?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35187032/