2017-02-03 122 views
12

我迁移库项目的.NET标准,我得到以下编译错误,当我尝试使用System.Reflection API调用Type:GetProperties()类型不包含定义“的GetProperties”

类型不包含定义 '的GetProperties'

这是我的project.json

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

我错过了什么?

回答

9

写这篇文章,GetProperties()现在是:

typeof(Object).GetTypeInfo().DeclaredProperties;

9

更新:与.NET核心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

首先调用GetTypeInfoType得到TypeInfo实例:

typeof(Object).GetTypeInfo().GetProperties(); 

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

+0

没错。但我认为你的答案有一个错字。它是'typeof(Object).GetTypeInfo()。GetProperties();' –

+0

@MiguelGamboa是的,我的坏,编辑 – Set