Which one do you prefer for testing if an expression is a ConstantExpression
? From the NodeType
property or a cast, and why?
public static bool IsConstantExpression(Expression expression)
{
return expression.NodeType == ExpressionType.Constant;
return expression is ConstantExpression;
}
One difference is expression.NodeType == ExpressionType.Constant
will throw an exception if expression
is null. I'm pretty sure ConstantExpression
is nullable, so that statement would be valid still.
我认为进行属性访问比让运行时检查对象的类型更好。
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.