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差异的详细信息.

相关文章

目录简介使用JS互操作使用ClipLazor库创建项目使用方法简单测...
目录简介快速入门安装 NuGet 包实体类User数据库类DbFactory...
本文实现一个简单的配置类,原理比较简单,适用于一些小型项...
C#中Description特性主要用于枚举和属性,方法比较简单,记录...
[TOC] # 原理简介 本文参考[C#/WPF/WinForm/程序实现软件开机...
目录简介获取 HTML 文档解析 HTML 文档测试补充:使用 CSS 选...