如何从 UnaryExpression 获取类型?
How can I get the Type from a UnaryExpression?
我有一个 属性 定义为 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
}
我有一个 属性 定义为 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
}