2014-06-13 25 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差异的详细信息。