Reflection in the .NET Framework for Windows Store Apps
Starting with the .NET Framework 4.5, the .NET Framework includes a set of reflection types and members for use in Windows 8.x 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 8.x Store app, you must use the reflection types and members in the .NET for Windows 8.x 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 8.x 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 8.x 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 8.x 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 8.x Store app, access to some .NET Framework types and members is restricted. For example, you cannot call .NET Framework methods that are not included in .NET for Windows 8.x Store apps, by using a MethodInfo object. In addition, certain types and members that are not considered safe within the context of a Windows 8.x Store app are blocked, as are Marshal and WindowsRuntimeMarshal members. This restriction affects only .NET Framework types and members; you can call your code or third-party code as you normally would.
This example uses the reflection types and members in the .NET for Windows 8.x Store apps to retrieve the methods and properties of the Calendar type, including inherited methods and properties. To run this code, paste it into the code file for a Windows 8.x Store page that contains a Windows.UI.Xaml.Controls.Textblock control named
textblock1 in a project named Reflection. If you paste this code inside a project with a different name, just make sure you change the namespace name to match your project.
Imports Windows.UI.Xaml.Navigation Imports System.Reflection Imports System.Globalization Imports System.Text Imports System Public NotInheritable Class MainPage Inherits Page Protected Overrides Sub OnNavigatedTo(e As NavigationEventArgs) Dim t As TypeInfo = GetType(Calendar).GetTypeInfo() Dim pList As IEnumerable(Of PropertyInfo) = t.DeclaredProperties Dim mList As IEnumerable(Of MethodInfo) = t.DeclaredMethods Dim sb As New StringBuilder() sb.Append("Properties:") For Each p As PropertyInfo In pList sb.Append((vbLf + p.DeclaringType.Name & ": ") + p.Name) Next sb.Append(vbLf & "Methods:") For Each m As MethodInfo In mList sb.Append((vbLf + m.DeclaringType.Name & ": ") + m.Name) Next textblock1.Text = sb.ToString() End Sub End Class