我正试图为Windows8.1和Windows 8.1制作一个Windows通用应用程序。
下面是我的问题的一个示例类,我使用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 2013。
发布于 2014-06-13 03:51:38
您应该使用DeclaredFields属性获取字段,使用DeclaredProperties获取属性。随着.NET框架的发展,反射API经历了一些成长的痛苦。MSDN的信息似乎是不准确的。简而言之,在.NET for Windows应用程序中,TypeInfo继承了MemberInfo not Type,因此它不能包含继承的成员GetFields()和GetProperties()。虽然Get*和声明的*成员都存在于完整框架中,但是对于Windows应用程序,您必须使用声明的*API。这个article详细介绍了反射API在不同风格的.NET框架中的差异。
https://stackoverflow.com/questions/24197300
复制相似问题