加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – Reflection.Typeinfo / Reflection.Type没有GetProperties

发布时间:2020-12-15 08:45:13 所属栏目:百科 来源:网络整理
导读:我正在尝试为 Windows 8.1和 Windows Phone 8.1制作Windows通用应用程序. 这是我的问题的一个示例类,我使用int类型作为示例,但无论我使用哪个类,都会出现错误: using System;using System.Collections.Generic;using System.Reflection;using System.Text;n
我正在尝试为 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差异的详细信息.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读