attributes in the metadata to be useful, you need a way to access
them, ideally during runtime. The classes in the
Reflection namespace, along with the
System.TypedReference classes, provide support for
examining and interacting with the metadata.
Reflection is generally used for any of four tasks:
- Viewing metadata
This might be used by tools and utilities that wish to display metadata.
- Performing type discovery
- Late binding to methods and properties
This allows the programmer to invoke properties and methods on objects dynamically instantiated, based on type discovery. This is also known as dynamic invocation.
- Creating types at runtime (reflection emit)
The ultimate use of reflection is to create new types at runtime and then to use those types to perform tasks. You might do this when a custom class, created at runtime, will run significantly faster than more generic code created at compile time. An example is offered later in this chapter.
In this section, you will use the C# reflection support to read the
metadata in the
Start by initializing an object of the type
MemberInfo. This object, in the