我如何从UnaryExpression获得类型



我有一个属性定义为Expression<Func<Kitten, object>>c => c.KittenAge,其中KittenAgeint?

我想知道这个的类型

我的代码是:
Expression<Func<Kitten, object>> property = c => c.KittenAge;
var unaryExpression = property.Body as UnaryExpression;
if (Nullable.GetUnderlyingType(unaryExpression.Type) == null)
{
   // Error, must be nullable
}

不幸的是,错误行总是被击中,因为TypeSystem.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
    }

相关内容

  • 没有找到相关文章

最新更新