C H A P T E R 15
Type Reflection, Late Binding, and
Attribute-Based Programming
As shown in Chapter 14, assemblies are the basic unit of deployment in the .NET universe. Using the
integrated object browsers of Visual Studio (and numerous other IDEs), you are able to examine the
types within a project’s referenced set of assemblies. Furthermore, external tools such as ildasm.exe
allow you to peek into the underlying CIL code, type metadata, and assembly manifest for a given .NET
binary. In addition to this design-time investigation of .NET assemblies, you are also able to
programmatically obtain this same information using the System.Reflection namespace. To this end,
the first task of this chapter is to define the role of reflection and the necessity of .NET metadata.
The remainder of the chapter examines a number of closely related topics, all of which hinge upon
reflection services. For example, you’ll learn how a .NET client may employ dynamic loading and late
binding to activate types it has no compile-time knowledge of. You’ll also learn how to insert custom
metadata into your .NET assemblies through the use of system-supplied and custom attributes. To put
all of these (seemingly esoteric) topics into perspective, the chapter closes by demonstrating how to
build several “snap-in objects” that you can plug into an extendable desktop GUI application.
The Necessity of Type Metadata
The ability to fully describe types (classes, interfaces, structures, enumerations, and delegates) using
metadata is a key element of the .NET platform. Numerous .NET technologies, such as Windows
Communication Foundation (WCF), and object serialization require the ability to discover the format of
types at runtime. Furthermore, cross-language interoperability, numerous compiler services, and an
IDE’s IntelliSense capabilities all rely on a concrete description of type.
Recall that the ildasm.exe utility allows you to view an assembly’s type metadata using the Ctrl+M
keyboard option (see Chapter 1). Thus, if you were to open any of the *.dll or *.exe assemblies created
over the course of this book (such as the Car