Reflection

Microsoft Silverlight will reach end of support after October 2021. Learn more.

Reflection provides objects that encapsulate assemblies, modules, and types. In Silverlight, as in other versions of the .NET Framework, you can use reflection to determine the type and members of an unknown object and to invoke the accessible members. You can use reflection to explore the types available in an assembly, to dynamically create an instance of a type, and to invoke the accessible methods, fields, and properties of the instance; however, in Silverlight reflection cannot be used to bypass access level restrictions and (for example) invoke private members.

The classes in the System.Reflection namespace, used with System.Type, support reflection. The following table describes typical uses of these classes.

Class

Typical uses

How to obtain

Assembly

Load assemblies, locate types in assemblies.

Load, GetExecutingAssembly, Type.Assembly

Module

Locate types in modules, resolve metadata tokens.

Type.Module, MemberInfo.Module

Type

Invoke members. Discover attributes, access modifiers, nesting, members.

Assembly.GetTypes, Module.GetTypes, GetType, Assembly.GetType

ConstructorInfo

Invoke (to create objects). Discover attributes, parameters, access modifiers.

Type.GetConstructors, Type.GetConstructor

MethodInfo

Invoke. Discover attributes, parameters, access modifiers.

Type.GetMethods, Type.GetMethod

FieldInfo

Get or set values. Discover attributes, parameters, access modifiers.

Type.GetFields, Type.GetField

EventInfo

Hook up events. Discover attributes, parameters, access modifiers.

Type.GetEvents, Type.GetEvent

PropertyInfo

Get or set values. Discover attributes, index parameters, access modifiers, read/write.

Type.GetProperties, Type.GetProperty

ParameterInfo

Discover type, position, ref/out.

MethodBuilder.GetParameters, ConstructorBuilder.GetParameters

The classes of the System.Reflection.Emit namespace provide a specialized form of reflection that enables you to build types and dynamic methods at run time.

Reflection can be used to create applications called type browsers, which enable users to select types and then view the information about those types.

In This Section

Topic

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 System.Type class and provides code examples that illustrate how to use System.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 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 the members of types.

Dynamically Loading and Using Types

Describes the reflection custom-binding interface that supports late binding.

Accessing Default Members

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

Accessing Custom Attributes

Demonstrates how to use reflection to query attribute existence and values.

Specifying Fully Qualified Type Names

Describes the format of fully qualified type names in 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. Explains how to create an event-handling method at run time using DynamicMethod.

Reference

Topic

Description

Type

The class that represents types in the runtime type system and provides core functionality for reflection.

System.Reflection

The namespace that contains other abstractions for code entities, such as the MethodInfo class that represents methods.

System.Reflection.Emit

The namespace that contains classes for generating dynamic assemblies and dynamic methods.

Topic

Description

Emitting Dynamic Methods and Assemblies

Describes the types in the System.Reflection.Emit namespace that enable an application to emit metadata and Microsoft intermediate language (MSIL) at run time.