2014-06-13 28 views
6

我正在嘗試製作Windows通用應用程序,用於Windows 8.1和Windows Phone 8.1。Reflection.Typeinfo/Reflection.Type沒有GetProperties/GetFields方法

下面是一個例子類,我的問題,我現在用的是int類型爲例,但錯誤是存在的,無論類的使用:

using System; 
using System.Collections.Generic; 
using System.Reflection; 
using System.Text; 

namespace myTtrpgHelper 
{ 
    class testClass 
    { 
     void testMethod() 
     { 
      int c = new int(); 
      Type type = c.GetType(); 
      TypeInfo typeInfo = IntrospectionExtensions.GetTypeInfo(type); 
      PropertyInfo[] p = typeInfo.GetProperties(); 
      PropertyInfo[] p2 = type.getProperties(); 

      PropertyInfo[] p3 = typeInfo.GetFields(); 
      PropertyInfo[] p4 = type.GetFields(); 
     } 
    } 
} 

的的GetProperties,和GetFields都顯示錯誤:

'System.Reflection.TypeInfo' does not contain a definition for 'GetFields' and no extension method 'GetFields' accepting a first argument of type 'System.Reflection.TypeInfo' could be found (are you missing a using directive or an assembly reference?) 

MSDN的頁面http://msdn.microsoft.com/en-us/library/system.reflection.typeinfo.aspx說,它應該得到支持,我使用的Visual Studio 2013年

+2

我編輯了你的標題。請參閱:「[應該在其標題中包含」標籤「](http://meta.stackexchange.com/questions/19190/)」,其中的共識是「不,他們不應該」。 –

回答

6

您應該使用DeclaredFields屬性獲取字段並DeclaredProperties獲取屬性。隨着.NET Framework的發展,Reflection API已經經歷了一些成長的痛苦。 MSDN信息似乎不準確。簡而言之,在Windows Store應用程序的.NET中,TypeInfo從MemberInfo繼承而不是Type,因此它不能包含繼承成員GetFields()GetProperties()。雖然Get *和Declared *成員都存在於完整的Framework中,但對於Windows Store應用程序,您必須使用Declared * API。這個article具有關於各種風格的.NET Framework中反射API差異的詳細信息。