Reflection in the .NET Framework

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:

  • 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.

  • 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.

  • 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. Use the GetConstructors or GetConstructor method of a Type to invoke a specific constructor.

  • 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. Use the GetMethods or GetMethod method of a Type to invoke a specific method.

  • 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.

  • 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.

  • 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.

  • 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.

  • Use CustomAttributeData to discover information about custom attributes when you are working in the reflection-only context of an application domain. CustomAttributeData allows you to examine attributes without creating instances of them.

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. Compilers for languages such as JScript use reflection to construct symbol tables. The classes in the System.Runtime.Serialization namespace use reflection to access data and to determine which fields to persist. The classes in the System.Runtime.Remoting namespace use reflection indirectly through serialization.

Title

Description

Runtime Types in Reflection

Describes the internal types, such as RuntimeType, that inherit the abstract classes in the System.Reflection namespace and provide much of their implementation.

Viewing Type Information

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.

Design Patterns Used by Reflection Classes

Provides a table showing the method naming pattern and use of the most frequently used reflection classes, such as the Module, Type, and MemberInfo classes.

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 Default Members

Demonstrates how to use reflection to access default members that a class might have.

Accessing Default Argument Values

Demonstrates how to use reflection to access arguments that have default values, and how to invoke methods that have arguments with default values.

Accessing Custom Attributes

Demonstrates using reflection to query attribute existence and values.

Specifying Fully Qualified Type Names

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.

Unmanaged Reflection API

Describes where to find information on using unmanaged reflection to examine metadata.

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. 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.

Was this page helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft