适用于 Windows 应用商店应用的 .NET Framework 中的反射

从 .NET Framework 4.5 开始,.NET Framework 包括一组反射类型和成员,用于 Windows 8.x 应用商店应用。 这些类型和成员可在完整的 .NET Framework 以及适用于 Windows 应用商店应用的 .NET 中使用。 本文档介绍 .NET Framework 4 和早期版本中这些和它们的对应项之间的主要差异。

如果要创建 Windows 8.x 应用商店应用,则必须在适用于 Windows 8.x 应用商店应用的 .NET 中使用反射类型和成员。 这些类型和成员在桌面应用中也可用,但不需要,因此你可以对这两种类型的应用使用相同的代码。

TypeInfo 和程序集加载

在适用于 Windows 8.x 应用商店应用的 .NET 中,该 TypeInfo 类包含 .NET Framework 4 Type 类的一些功能。 对象 Type 表示对类型定义的引用,而 TypeInfo 对象表示类型定义本身。 这使你可以操作 Type 对象,而无需运行时加载它们所引用的程序集。 获取关联的 TypeInfo 对象会强制程序集加载。

TypeInfo 包含许多在 Type 上可用的成员,并且适用于 Windows 8.x 应用商店应用的 .NET 中的许多反射属性返回 TypeInfo 对象的集合。 若要从TypeInfo对象获取Type对象,请使用GetTypeInfo该方法。

查询方法

在适用于 Windows 8.x 应用商店应用的 .NET 中,使用返回 IEnumerable<T> 集合的反射属性,而不是返回数组的方法。 反射上下文可以实现这些大型程序集或类型集合的延迟遍历。

反射属性仅返回特定对象上声明的方法,而不是遍历继承树。 此外,它们不使用 BindingFlags 参数进行筛选。 相反,通过使用对返回的集合的 LINQ 查询,筛选将发生在用户代码中。 对于源自运行时的反射对象(例如作为typeof(Object)的结果),最好使用RuntimeReflectionExtensions类的辅助方法来遍历继承树。 自定义反射上下文中的对象的使用者不能使用这些方法,并且必须遍历继承树本身。

限制

在 Windows 8.x 应用商店应用中,对某些 .NET Framework 类型和成员的访问受到限制。 例如,不能使用 MethodInfo 对象调用 .NET for Windows 8.x 应用商店应用中不包含的 .NET Framework 方法。 除此之外,在 Windows 8.x 应用商店应用的上下文中,被认为不安全的特定类型和成员是受阻的,就像 MarshalWindowsRuntimeMarshal 成员一样。 此限制仅影响 .NET Framework 类型和成员;可以像平常一样调用代码或第三方代码。

示例:

此示例使用适用于 Windows 8.x 应用商店应用的 .NET 中的反射类型和成员来检索 Calendar 类型的方法和属性,包括继承来的方法和属性。 若要运行此代码,请在名为“反射”的项目中,将代码粘贴到包含名为 Windows.UI.Xaml.Controls.TextBlocktextblock1 控件的 Windows 8.x 应用商店页的代码文件中。 如果将此代码粘贴到具有不同名称的项目中,只需确保更改命名空间名称以匹配项目。

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();
        }
    }
}
Imports Windows.UI.Xaml.Navigation
Imports System.Reflection
Imports System.Globalization
Imports System.Text

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

另请参阅