Chapter 10: Metadata and Reflection

Chapter 10: Metadata and Reflection

Overview

Metadata, which is often described as data about data, formalizes the composition of an assembly. Metadata is the actualization of the state of an assembly. As a reflection of an assembly, metadata includes all information pertaining to the assembly, including a detailed description of each type, the attributes of the assembly, and other particulars of the assembly itself. Metadata is similar to a type library in COM, except that metadata is always persisted in the application that it describes. For this reason, assemblies are often referred to as self-describing. Because metadata is indigenous to the assembly, metadata cannot be lost, and versioning problems are avoided. Metadata is emitted primarily by managed language compilers and consumed by metadata browsers, other .NET tools, and managed applications. The Common Language Runtime (CLR) uses metadata extensively. Just-in-time compilation, code access security, garbage collection, and other services of the CLR rely heavily on metadata. Once emitted, metadata is read-only.

Metadata is important to anyone programming in the managed environment. Assembly inspection, late binding, and advanced concepts such as self-generating code require a nontrivial understanding of metadata. You can interrogate metadata by using reflection. Reflection facilitates late binding and other means of leveraging metadata. Most important, mastery of metadata promotes a better understanding of the managed world, which (one hopes) translates into better-written code.