Export (0) Print
Expand All
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. For example, you cannot call .NET Framework methods that are not included in .NET for Windows Store apps, by using a MethodInfo object. In addition, certain types and members that are not considered safe within the context of a Windows 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 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 Store page that contains a Windows.UI.Xaml.Controls.Textblock control named textblock1.

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Navigation;
using System.Reflection;
using System.Globalization;
using System.Text;


namespace Reflection
{
    public sealed partial class MainPage : Page
    {
        public MainPage()
        {
           this.InitializeComponent();
        }

        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            TypeInfo t = typeof(Calendar).GetTypeInfo();
            IEnumerable<PropertyInfo> pList = t.DeclaredProperties;
            IEnumerable<MethodInfo> mList = t.DeclaredMethods;

            StringBuilder sb = new StringBuilder();

            sb.Append("Properties:");
            foreach (PropertyInfo p in pList)
            {

                sb.Append("\n" + p.DeclaringType.Name + ": " + p.Name);
            }
            sb.Append("\nMethods:");
            foreach (MethodInfo m in mList)
            {
                sb.Append("\n" + m.DeclaringType.Name + ": " + m.Name);
            }

            textblock1.Text = sb.ToString();

        }
    }
}
Show:
© 2014 Microsoft. All rights reserved.