简体   繁体   English

Reflection.Typeinfo / Reflection.Type没有GetProperties / GetFields方法

[英]Reflection.Typeinfo/Reflection.Type does not have GetProperties/GetFields method

I am trying to make a Windows Universal App, for Windows 8.1 and Windows Phone 8.1. 我正在尝试为Windows 8.1和Windows Phone 8.1制作Windows通用应用程序。

Here is an example class of my problem, I am using the type int as an example, but the error is there regardless of the class I use: 这是我的问题的一个示例类,我使用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();
        }
    }
}

The GetProperties, and GetFields both display errors: 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?) 

The msdn page http://msdn.microsoft.com/en-us/library/system.reflection.typeinfo.aspx says it should be supported, I am using visual studio 2013. msdn页面http://msdn.microsoft.com/en-us/library/system.reflection.typeinfo.aspx表示应该支持它,我正在使用visual studio 2013。

You should use the DeclaredFields property to get the fields and DeclaredProperties to get the properties. 您应该使用DeclaredFields属性来获取字段,使用DeclaredProperties来获取属性。 The Reflection APIs have gone through some growing pains as the .NET Framework has evolved. 随着.NET Framework的发展,Reflection API经历了一些成长的烦恼。 The MSDN info seems to be inaccurate. MSDN信息似乎不准确。 In short, in .NET for Windows Store apps, TypeInfo inherits from MemberInfo not Type so it cannot contain the inherited members GetFields() and GetProperties() . 简而言之,在.NET for Windows Store应用程序中,TypeInfo继承自MemberInfo而不是Type,因此它不能包含继承的成员GetFields()GetProperties() While both Get* and Declared* members exist in the full Framework, for the Windows Store apps, you have to use the Declared* APIs. 虽然Get *和Declared *成员都存在于完整的Framework中,但对于Windows应用商店应用,您必须使用Declared * API。 This article has detailed information on the differences in the Reflection APIs in various flavors of the .NET Framework. 本文详细介绍了各种.NET Framework中Reflection API的不同之处。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM