Reflection.Typeinfo / Reflection.Type does not have a 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 use the int type as an example, but the error exists regardless of the class that 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(); } } } 

GetProperties and GetFields 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 that it should be supported, I am using visual studio 2013.

+6
source share
1 answer

You must use the DeclaredFields property to get the fields and DeclaredProperties to get the properties. Reflection APIs have gone through some growing pains since the .NET Framework evolved. MSDN information seems inaccurate. In short, in .NET for Windows Store applications, TypeInfo does not inherit from TypeInfo from MemberInfo, so it cannot contain the inherited members GetFields() and GetProperties() . Although Get * and Declared * members exist in the full version of the Framework, for Windows Store applications you need to use the Declared * APIs. This article provides detailed information on the differences in the Reflection APIs in the various versions of the .NET Framework.

+6
source

Source: https://habr.com/ru/post/970792/


All Articles