Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

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:

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:

'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.

like image 673
Tain Avatar asked Jun 13 '14 03:06

Tain


1 Answers

You should use the DeclaredFields property to get the fields and DeclaredProperties to get the properties. The Reflection APIs have gone through some growing pains as the .NET Framework has evolved. The MSDN info seems to be inaccurate. In short, in .NET for Windows Store apps, TypeInfo inherits from MemberInfo not Type so it cannot contain the inherited members GetFields() and GetProperties(). While both Get* and Declared* members exist in the full Framework, for the Windows Store apps, you have to use the Declared* APIs. This article has detailed information on the differences in the Reflection APIs in various flavors of the .NET Framework.

like image 195
Mike Zboray Avatar answered Oct 05 '22 22:10

Mike Zboray