Chapter 13

Reflection

Reflection is a generic term that describes the ability to inspect and manipulate program elements at runtime. For example, reflection allows you to:

  • Enumerate the members of a type
  • Instantiate a new object
  • Execute the members of an object
  • Find out information about a type
  • Find out information about an assembly
  • Inspect the custom attributes applied to a type
  • Create and compile a new assembly

This list represents a great deal of functionality and encompasses some of the most powerful and complex capabilities provided by the .NET Framework class library. Although this chapter does not have the space to cover all the capabilities of reflection, it focuses on those elements that you are likely to use most frequently.

This chapter is about:

  • Custom attributes, a mechanism that allows you to associate custom metadata with program elements. This metadata is created at compile time and embedded in an assembly.
  • Inspecting the metadata at runtime using some of the capabilities of reflection.
  • Some of the fundamental classes that enable reflection, including the System.Type and System.Reflection.Assembly classes, which provide the access points for much of what you can do with reflection.

To demonstrate custom attributes and reflection, you develop an example based on a company that regularly ships upgrades of its software and wants to have details of these upgrades documented automatically. In the example, you define custom attributes that indicate the date when ...

Get Professional C# 2008 now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.