首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Reflection.ypeinfo/Reflection.Type没有GetProperties/GetField方法

Reflection.ypeinfo/Reflection.Type没有GetProperties/GetField方法
EN

Stack Overflow用户
提问于 2014-06-13 03:35:08
回答 1查看 5.2K关注 0票数 6

我正试图为Windows8.1和Windows 8.1制作一个Windows通用应用程序。

下面是我的问题的一个示例类,我使用int类型作为示例,但是无论我使用的类是什么,都存在错误:

代码语言:javascript
运行
复制
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都显示错误:

代码语言:javascript
运行
复制
'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。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 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框架中的差异。

票数 6
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/24197300

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档