if (SelectedOperator is StringOperators){ MethodInfo method; var value = Expression.Constant(Value); switch ((StringOperators)SelectedOperator) { case StringOperators.Is: condition = Expression.Equal(property,value); break; case StringOperators.IsNot: condition = Expression.NotEqual(property,value); break; case StringOperators.StartsWith: method = typeof(string).getmethod("StartsWith",new[] { typeof(string) }); condition = Expression.Call(property,method,value); break; case StringOperators.Contains: method = typeof(string).getmethod("Contains",value); break; case StringOperators.EndsWith: method = typeof(string).getmethod("EndsWith",value); break; }}解决方法 使用
AndAlso
向结果表达式添加空检查,如下所示: // Your switch stays as isswitch ((StringOperators)SelectedOperator) { case StringOperators.Is: condition = Expression.Equal(property,value); break; ...}// Create null checker property != nullvar nullCheck = Expression.NotEqual(property,Expression.Constant(null,typeof(object)));// Add null checker in front of the condition using &&condition = Expression.AndAlso(nullCheck,condition);总结
以上是内存溢出为你收集整理的c# – 对lambda表达式树进行空检查全部内容,希望文章能够帮你解决c# – 对lambda表达式树进行空检查所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)