1 out of 2 rated this helpful - Rate this topic

Reflection in the .NET Framework for Windows Store Apps

.NET Framework 4.5

Starting with the .NET Framework 4.5, the .NET Framework includes a set of reflection types and members for use in Windows Store apps. These types and members are available in the full .NET Framework as well as in the .NET for Windows Store apps. This document explains the major differences between these and their counterparts in the .NET Framework 4 and earlier versions.

If you are creating a Windows Store app, you must use the reflection types and members in the .NET for Windows Store apps. These types and members are also available, but not required, for use in desktop apps, so you can use the same code for both types of apps.

In the .NET for Windows Store apps, the TypeInfo class contains some of the functionality of the .NET Framework 4 Type class. A Type object represents a reference to a type definition, whereas a TypeInfo object represents the type definition itself. This enables you to manipulate Type objects without necessarily requiring the runtime to load the assembly they reference. Getting the associated TypeInfo object forces the assembly to load.

TypeInfo contains many of the members available on Type, and many of the reflection properties in the .NET for Windows Store apps return collections of TypeInfo objects. To get a TypeInfo object from a Type object, use the GetTypeInfo method.

In the .NET for Windows Store apps, you use the reflection properties that return IEnumerable<T> collections instead of methods that return arrays. Reflection contexts can implement lazy traversal of these collections for large assemblies or types.

The reflection properties return only the declared methods on a particular object instead of traversing the inheritance tree. Moreover, they do not use BindingFlags parameters for filtering. Instead, filtering takes place in user code, by using LINQ queries on the returned collections. For reflection objects that originate with the runtime (for example, as the result of typeof(Object)), traversing the inheritance tree is best accomplished by using the helper methods of the RuntimeReflectionExtensions class. Consumers of objects from customized reflection contexts cannot use these methods, and must traverse the inheritance tree themselves.

In a Windows Store app, access to some .NET Framework types and members is restricted. You cannot call .NET Framework methods that are not included in .NET for Windows Store apps, for example, by using a MethodInfo object. In addition, certain unsafe types and members are blocked, as are Marshal and WindowsRuntimeMarshal members. This restriction affects only .NET Framework types and members; you can call any user or third-party code as you normally would.

This example uses the reflection types and members in the .NET for Windows Store apps to retrieve the methods and properties of the AdapterDictionary type, including inherited methods and properties. A LINQ query is used to filter the collection to return only family methods. To run this code, paste it into the main method of a console application.

            Type t = typeof(System.Web.Configuration.AdapterDictionary);
            IEnumerable<PropertyInfo> pList = t.GetRuntimeProperties(); //Get all properties, including inherited properties.
            IEnumerable<MethodInfo> mList = t.GetRuntimeMethods(); //Get all methods, including inherited methods.
           //To get only declared properties, use t.GetTypeInfo().DeclaredProperties



            Console.WriteLine("Properties:");
            foreach (PropertyInfo p in pList) {
                
                Console.WriteLine(p.DeclaringType.Name + ": " + p.Name);
            }
            Console.WriteLine("\nMethods:");
            foreach (MethodInfo m in mList)
            {
                Console.WriteLine(m.DeclaringType.Name + ": " + m.Name);
            }
            Console.WriteLine("\nFamily Methods Only:");
            foreach (MethodInfo m in mList.Where<MethodInfo>(x=>x.IsFamily)) //Filter for family methods only
            {
                
                Console.WriteLine(m.DeclaringType.Name + ": " + m.Name);
            }
            Console.ReadLine();
Did you find this helpful?
(1500 characters remaining)
© 2013 Microsoft. All rights reserved.