.NET 中的反射Reflection in .NET

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.

公共语言运行时加载程序管理应用程序域,应用程序域构成具有相同应用程序范围的对象周围定义的边界。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). 相反,你使用由公共语言运行时 (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.

TitleTitle 描述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 描述 Backus-Naur 形式 (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