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

从 .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 对象的集合。 若要从 Type 对象获取 TypeInfo 对象,请使用 GetTypeInfo 方法。

查询方法

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

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

限制

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

示例

此示例使用适用于 Windows 8.x 应用商店应用的 .NET 中的反射类型和成员来检索 Calendar 类型的方法和属性,包括继承的方法和属性。 若要运行此代码,请在名为“反射”的项目中,将代码粘贴到包含名为 textblock1Windows.UI.Xaml.Controls.TextBlock 控件的 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

请参阅