c# – Reflection.Typeinfo / Reflection.Type没有GetProperties / GetFields方法

我正在尝试为 Windows 8.1和 Windows Phone 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和GetField都显示错误

'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.

解决方法

您应该使用DeclaredFields属性获取字段,使用DeclaredProperties来获取属性.随着.NET Framework的发展,Reflection API经历了一些成长的烦恼. MSDN信息似乎不准确.简而言之,在.NET for Windows Store应用程序中,TypeInfo继承自MemberInfo而不是Type,因此它不能包含继承的成员GetFields()和GetProperties().虽然Get *和Declared *成员都存在于完整的Framework中,但对于Windows应用商店应用,您必须使用Declared * API.这个 article提供了有关各种版本的.NET Framework中的Reflection API差异的详细信息.

相关文章

原文地址:http://msdn.microsoft.com/en-us/magazine/cc163...
前言 随着近些年微服务的流行,有越来越多的开发者和团队所采...
最近因为比较忙,好久没有写博客了,这篇主要给大家分享一下...
在多核CPU在今天和不久的将来,计算机将拥有更多的内核,Mic...
c语言输入成绩怎么判断等级
字符型数据在内存中的存储形式是什么