.NET Framework 中的反映Reflection in the .NET Framework

System.Reflection 命名空間中的類別,連同 System.Type,可讓您取得已載入組件和其中所定義類型的資訊,例如類別介面實值型別The classes in the System.Reflection namespace, together with System.Type, enable you to obtain information about loaded assemblies and the types defined within them, such as classes, interfaces, and value types. 您也可以使用反映在執行階段建立類型執行個體,並叫用和存取它們。You can also use reflection to create type instances at run time, and to invoke and access them. 如需反映特定層面的主題,請參閱此概觀結尾的相關主題For topics about specific aspects of reflection, see Related Topics at the end of this overview.

Common Language Runtime 載入器會管理應用程式定義域,這會在有相同應用程式範圍的物件周圍構成定義的界限。The common language runtime loader manages application domains, which constitute defined boundaries around objects that have the same application scope. 這個管理包含載入每個組件至適當的應用程式定義域和控制每個組件內類型階層的記憶體配置。This management includes loading each assembly into the appropriate application domain and controlling the memory layout of the type hierarchy within each assembly.

組件包含模組、模組包含類型,而類型包含成員。Assemblies contain modules, modules contain types, and types contain members. 反映提供封裝組件、模組和類型的物件。Reflection provides objects that encapsulate assemblies, modules, and types. 您可以使用反映來動態建立類型的執行個體、繫結類型至現有的物件,或從現有的物件取得類型。You can use reflection to dynamically create an instance of a type, bind the type to an existing object, or get the type from an existing object. 然後,您可以叫用類型的方法或存取其欄位和屬性。You can then invoke the type's methods or access its fields and properties. 反映的一般用法包含下列幾項:Typical uses of reflection include the following:

  • 使用 Assembly 來定義和載入組件,載入列在組件資訊清單中的模組,然後從這個組件中尋找類型並建立它的執行個體。Use Assembly to define and load assemblies, load modules that are listed in the assembly manifest, and locate a type from this assembly and create an instance of it.

  • 使用 Module 來探索資訊,例如包含模組的組件以及模組中的類別。Use Module to discover information such as the assembly that contains the module and the classes in the module. 您也可以取得所有全域方法,或在模組上所定義的其他特定非全域方法。You can also get all global methods or other specific, nonglobal methods defined on the module.

  • 使用 ConstructorInfo 來探索資訊,例如名稱、參數、存取修飾詞 (例如 publicprivate),以及建構函式的實作詳細資料 (例如 abstractvirtual)。Use ConstructorInfo to discover information such as the name, parameters, access modifiers (such as public or private), and implementation details (such as abstract or virtual) of a constructor. 使用 TypeGetConstructorsGetConstructor 方法來叫用特定的建構函式。Use the GetConstructors or GetConstructor method of a Type to invoke a specific constructor.

  • 使用 MethodInfo 來探索資訊,例如名稱、傳回類型、參數、存取修飾詞 (例如 publicprivate),以及方法的實作詳細資料 (例如 abstractvirtual)。Use MethodInfo to discover information such as the name, return type, parameters, access modifiers (such as public or private), and implementation details (such as abstract or virtual) of a method. 使用 TypeGetMethodsGetMethod 方法來叫用特定的方法。Use the GetMethods or GetMethod method of a Type to invoke a specific method.

  • 使用 FieldInfo 來探索資訊,例如名稱、存取修飾詞 (例如 publicprivate),以及欄位的實作詳細資料 (例如 static),並取得或設定欄位值。Use FieldInfo to discover information such as the name, access modifiers (such as public or private) and implementation details (such as static) of a field, and to get or set field values.

  • 使用 EventInfo 來探索資訊,例如名稱、事件處理常式資料類型、自訂屬性、宣告類型,以及事件的反映類型,並加入或移除事件處理常式。Use EventInfo to discover information such as the name, event-handler data type, custom attributes, declaring type, and reflected type of an event, and to add or remove event handlers.

  • 使用 PropertyInfo 來探索資訊,例如名稱、資料類型、宣告類型、反映類型和唯讀或可寫入的屬性狀態,並且取得或設定屬性值。Use PropertyInfo to discover information such as the name, data type, declaring type, reflected type, and read-only or writable status of a property, and to get or set property values.

  • 使用 ParameterInfo 來探索資訊,例如參數的名稱、資料類型、參數是否為輸入或輸出參數以及方法簽章中的參數位置。Use ParameterInfo to discover information such as a parameter's name, data type, whether a parameter is an input or output parameter, and the position of the parameter in a method signature.

  • 當您在應用程式定義域的僅限反映之內容中工作時,請使用 CustomAttributeData 來探索自訂屬性的相關資訊。Use CustomAttributeData to discover information about custom attributes when you are working in the reflection-only context of an application domain. CustomAttributeData 可讓您檢查屬性而不需建立它們的執行個體。CustomAttributeData allows you to examine attributes without creating instances of them.

System.Reflection.Emit 命名空間的類別提供一種特殊形式的反映,可讓您在執行階段建置類型。The classes of the System.Reflection.Emit namespace provide a specialized form of reflection that enables you to build types at run time.

反映也可以用來建立稱為類型瀏覽器的應用程式,可讓使用者選取類型,然後檢視這些類型的相關資訊。Reflection can also be used to create applications called type browsers, which enable users to select types and then view the information about those types.

反映還有其他用途。There are other uses for reflection. JScript 之類的語言編譯器會使用反映來建構符號表。Compilers for languages such as JScript use reflection to construct symbol tables. System.Runtime.Serialization 命名空間中的類別會使用反映來存取資料,並決定要保存哪個欄位。The classes in the System.Runtime.Serialization namespace use reflection to access data and to determine which fields to persist. System.Runtime.Remoting 命名空間中的類別在序列化時會間接使用反映。The classes in the System.Runtime.Remoting namespace use reflection indirectly through serialization.

反映中的執行階段類型Runtime Types in Reflection

反映會提供類別,例如 TypeMethodInfo,表示類型、成員、參數和其他程式碼實體。Reflection provides classes, such as Type and MethodInfo, to represent types, members, parameters, and other code entities. 不過,當您使用反映時不直接搭配類別使用,則其中大部分都會是抽象的 (在 Visual Basic 中為 MustInherit)。However, when you use reflection you don't work directly with these classes, most of which are abstract (MustInherit in Visual Basic). 您可改用 Common Language Runtime (CLR) 所提供的類型。Instead, you work with types provided by the common language runtime (CLR).

例如,當您使用 C# typeof 運算子 (在 Visual Basic 中為 GetType) 來取得 Type 物件時,該物件確實為 RuntimeTypeFor example, when you use the C# typeof operator (GetType in Visual Basic) to obtain a Type object, the object is really a RuntimeType. RuntimeType 衍生自 Type,並提供所有抽象方法的實作。RuntimeType derives from Type, and provides implementations of all the abstract methods.

這些執行階段類別是 internal (在 Visual Basic 中為 Friend)。These runtime classes are internal (Friend in Visual Basic). 它們不會分別從其基底類別記錄,因為它們的行為由基底類別的文件所描述。They are not documented separately from their base classes, because their behavior is described by the base class documentation.

標題Title 說明Description
檢視類型資訊Viewing Type Information 描述 Type 類別,並提供程式碼範例說明如何搭配幾個反映類別來使用 Type,以取得建構函式、方法、欄位、屬性和事件的相關資訊。Describes the Type class and provides code examples that illustrate how to use Type with several reflection classes to obtain information about constructors, methods, fields, properties, and events.
反映和泛用類型Reflection and Generic Types 說明反映如何處理泛型類型和泛型方法的型別參數和型別引數。Explains how reflection handles the type parameters and type arguments of generic types and generic methods.
反映的安全性考量Security Considerations for Reflection 描述判斷可使用哪種程度的反映之規則,以探索類型資訊和存取類型。Describes the rules that determine to what degree reflection can be used to discover type information and access types.
動態載入和使用類型Dynamically Loading and Using Types 描述支援晚期繫結的反映自訂繫結介面。Describes the reflection custom-binding interface that supports late binding.
如何:將組件載入到僅限反映的內容將組件載入到僅限反映的內容How to: Load Assemblies into the Reflection-Only Context 描述僅限反映的載入內容。Describes the reflection-only load context. 示範如何載入組件、如何測試內容,以及如何檢查套用至僅限反映的內容中組件的屬性。Shows how to load an assembly, how to test the context, and how to examine attributes applied to an assembly in the reflection-only context.
存取自訂屬性Accessing Custom Attributes 示範如何使用反映來查詢屬性是否存在和屬性的值。Demonstrates using reflection to query attribute existence and values.
指定完整的類型名稱Specifying Fully Qualified Type Names 描述完整的類型名稱之格式,根據巴克斯格式 (BNF),以及指定特殊字元、組件名稱、指標、參考和陣列所需的語法來描述。Describes the format of fully qualified type names in terms of the Backus-Naur form (BNF), and the syntax required for specifying special characters, assembly names, pointers, references, and arrays.
如何:使用反映連結委派How to: Hook Up a Delegate Using Reflection 說明如何建立方法的委派,以及連結委派到事件。Explains how to create a delegate for a method and hook the delegate up to an event. 說明如何在執行階段使用 DynamicMethod 建立事件處理方法。Explains how to create an event-handling method at run time using DynamicMethod.
發出動態方法和組件Emitting Dynamic Methods and Assemblies 說明如何產生動態組件和動態方法。Explains how to generate dynamic assemblies and dynamic methods.

參考資料Reference

System.Type

System.Reflection

System.Reflection.Emit