2017-02-03 121 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