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

前端之家收集整理的这篇文章主要介绍了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差异的详细信息.
原文链接:https://www.f2er.com/csharp/244737.html

猜你在找的C#相关文章