適用於 Windows 市集應用程式之 .NET Framework 中的反映Reflection in the .NET Framework for Windows Store Apps

從 .NET Framework 4.5 開始,.NET Framework 包含一組反映類型和成員,以便用於 Windows 8.x 市集Windows 8.x Store 應用程式。Starting with the .NET Framework 4.5, the .NET Framework includes a set of reflection types and members for use in Windows 8.x 市集Windows 8.x Store apps. 這些類型和成員提供於完整 .NET Framework,以及適用於 Windows 市集應用程式的 .NETThese types and members are available in the full .NET Framework as well as in the .NET for Windows Store apps. 本文件說明這些與其對應項目在 .NET Framework 4 和舊版之間的主要差異。This document explains the major differences between these and their counterparts in the .NET Framework 4 and earlier versions.

如果您要建立 Windows 8.x 市集Windows 8.x Store 應用程式,必須使用 適用於 Windows 8.x 市集應用程式的 .NET.NET for Windows 8.x Store apps 中的反映類型和成員。If you are creating a Windows 8.x 市集Windows 8.x Store app, you must use the reflection types and members in the 適用於 Windows 8.x 市集應用程式的 .NET.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.

TypeInfo 和組件載入TypeInfo and Assembly Loading

適用於 Windows 8.x 市集應用程式的 .NET.NET for Windows 8.x Store apps 中,TypeInfo 類別包含 .NET Framework 4 Type 類別的某些功能。In the 適用於 Windows 8.x 市集應用程式的 .NET.NET for Windows 8.x Store apps, the TypeInfo class contains some of the functionality of the .NET Framework 4 Type class. Type 物件代表類型定義的參考,而 TypeInfo 物件則代表類型定義本身。A Type object represents a reference to a type definition, whereas a TypeInfo object represents the type definition itself. 這可讓您操作 Type 物件,而不一定需要執行階段載入它們所參考的組件。This enables you to manipulate Type objects without necessarily requiring the runtime to load the assembly they reference. 取得相關聯的 TypeInfo 物件會強制載入組件。Getting the associated TypeInfo object forces the assembly to load.

TypeInfo 包含許多 Type 上可用的成員,而 適用於 Windows 8.x 市集應用程式的 .NET.NET for Windows 8.x Store apps 中的許多反映屬性會傳回 TypeInfo 物件的集合。TypeInfo contains many of the members available on Type, and many of the reflection properties in the 適用於 Windows 8.x 市集應用程式的 .NET.NET for Windows 8.x Store apps return collections of TypeInfo objects. 若要從 Type 物件取得 TypeInfo 物件,請使用 GetTypeInfo 方法。To get a TypeInfo object from a Type object, use the GetTypeInfo method.

查詢方法Query Methods

適用於 Windows 8.x 市集應用程式的 .NET.NET for Windows 8.x Store apps 中,請使用會傳回 IEnumerable<T> 集合的反映屬性,而不要使用傳回陣列的方法。In the 適用於 Windows 8.x 市集應用程式的 .NET.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. 此外,它們不會使用 BindingFlags 參數進行篩選。Moreover, they do not use BindingFlags parameters for filtering. 相反地,篩選發生在使用者程式碼中,對傳回的集合使用 LINQ 查詢。Instead, filtering takes place in user code, by using LINQ queries on the returned collections. 對於因執行階段而產生的反映物件 (例如,因為 typeof(Object)),周遊繼承樹狀結構最佳的執行方式是使用 RuntimeReflectionExtensions 類別的 helper 方法。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.

限制Restrictions

Windows 8.x 市集Windows 8.x Store 應用程式中,對於某些 .NET Framework 類型和成員的存取會受到限制。In a Windows 8.x 市集Windows 8.x Store app, access to some .NET Framework types and members is restricted. 例如,您不能藉由使用 MethodInfo 物件,呼叫未包含在 適用於 Windows 8.x 市集應用程式的 .NET.NET for Windows 8.x Store apps 的 .NET Framework 方法。For example, you cannot call .NET Framework methods that are not included in 適用於 Windows 8.x 市集應用程式的 .NET.NET for Windows 8.x Store apps, by using a MethodInfo object. 此外,在 Windows 8.x 市集Windows 8.x Store 應用程式的內容中不被視為安全的特定類型和成員會被封鎖,就如同 MarshalWindowsRuntimeMarshal 成員一樣。In addition, certain types and members that are not considered safe within the context of a Windows 8.x 市集Windows 8.x Store app are blocked, as are Marshal and WindowsRuntimeMarshal members. 這項限制只會影響 .NET Framework 類型和成員;您可以如常呼叫您的程式碼或協力廠商程式碼。This restriction affects only .NET Framework types and members; you can call your code or third-party code as you normally would.

範例Example

此範例使用 適用於 Windows 8.x 市集應用程式的 .NET.NET for Windows 8.x Store apps 中的反映類型和成員來擷取 Calendar 類型的方法和屬性,包括繼承的方法和屬性。This example uses the reflection types and members in the 適用於 Windows 8.x 市集應用程式的 .NET.NET for Windows 8.x Store apps to retrieve the methods and properties of the Calendar type, including inherited methods and properties. 若要執行此程式碼,請將它貼入 Windows 8.x 市集Windows 8.x Store 頁面的程式碼檔案,該頁面在名為 Reflection 的專案中包含名為 textblock1Windows.UI.Xaml.Controls.TextBlock 控制項。To run this code, paste it into the code file for a Windows 8.x 市集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.

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

另請參閱See also