类型不包含 'GetProperties' 的定义

Type does not contain a definition for 'GetProperties'

我正在将库项目迁移到 .net 标准,当我尝试使用 System.Reflection API 调用 Type:GetProperties() 时出现以下编译错误:

Type does not contain a definition for 'GetProperties'

这是我的 project.json:

{
  "version": "1.0.0-*",
  "buildOptions": {
    "debugType": "portable"
  },
  "dependencies": {},
  "frameworks": {
    "netstandard1.6": {
      "dependencies": {
        "NETStandard.Library": "1.6.0"
      }
    }
  }
}

我错过了什么?

更新:随着 .NET COre 2.0 的发布,System.Type 回归,因此两个选项都可用:

  • typeof(Object).GetType().GetProperties()
  • typeof(Object).GetTypeInfo().GetProperties()

    这个需要加上using System.Reflection;

  • typeof(Object).GetTypeInfo().DeclaredProperties

    注意这个属性returnsIEnumerable<PropertyInfo>,不是前两种方法PropertyInfo[]


System.Type 上的大多数 reflection-related 成员现在都在 System.Reflection.TypeInfo 上。

首先调用 GetTypeInfoType:

中获取 TypeInfo 实例
typeof(Object).GetTypeInfo().GetProperties();

此外,不要忘记使用 using System.Reflection;

在撰写本文时,GetProperties() 现在是:

typeof(Object).GetTypeInfo().DeclaredProperties;