类型不包含'GetProperties'的定义

Mig*_*boa 16 .net-core .net-standard

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

类型不包含'GetProperties'的定义

这是我的project.json:

{
  "version": "1.0.0-*",
  "buildOptions": {
    "debugType": "portable"
  },
  "dependencies": {},
  "frameworks": {
    "netstandard1.6": {
      "dependencies": {
        "NETStandard.Library": "1.6.0"
      }
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

我错过了什么?

Set*_*Set 12

更新:随着.NET COre 2.0发布System.Type 回来,所以两个选项都可用:

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

    这个需要添加 using System.Reflection;

  • typeof(Object).GetTypeInfo().DeclaredProperties

    请注意,此属性返回IEnumerable<PropertyInfo>,而不是PropertyInfo[]前两个方法.


大多数与反思相关的成员System.Type现在都在System.Reflection.TypeInfo.

第一次调用GetTypeInfo从以下位置获取TypeInfo实例Type:

typeof(Object).GetTypeInfo().GetProperties();
Run Code Online (Sandbox Code Playgroud)

另外,别忘了使用 using System.Reflection;


rob*_*pim 10

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

typeof(Object).GetTypeInfo().DeclaredProperties;

  • `DeclaredProperties`只返回在类型本身上声明的属性,不包括从父类继承的属性 (3认同)