Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/clojure/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何使用表达式来检查=针对用户定义的数据类型是否为null?_C#_Lambda_Expression Trees - Fatal编程技术网

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
的对象?我怀疑这是真的。