我有一个属性定义为Expression<Func<Kitten, object>>
即c => c.KittenAge
,其中KittenAge
为int?
我想知道这个的类型
我的代码是:Expression<Func<Kitten, object>> property = c => c.KittenAge;
var unaryExpression = property.Body as UnaryExpression;
if (Nullable.GetUnderlyingType(unaryExpression.Type) == null)
{
// Error, must be nullable
}
不幸的是,错误行总是被击中,因为Type
是System.Object
。我如何从表达式中得到int?
的类型?
试试这个:
Expression<Func<Kitten, object>> property = c => c.KittenAge;
var unaryExpression = property.Body as UnaryExpression;
var propertyExpression = unaryExpression.Operand as MemberExpression;
if (Nullable.GetUnderlyingType(propertyExpression.Type) == null)
{
// Error, must be nullable
}