Introduction
The Common Language Runtime (CLR) stores metadata about assemblies, types, members, and other constructs in a series of structured metadata tables. These tables are the foundation for reflection, type safety, and the execution engine.
Key Metadata Tables
Table | Purpose | Primary Columns |
---|---|---|
Module | Defines each module in an assembly | Generation, Name, Mvid |
TypeDef | Describes each type defined in the module | Flags, Name, Namespace, Extends, FieldList, MethodList |
MethodDef | Holds information about each method | RVA, ImplFlags, Flags, Name, Signature, ParamList |
Field | Stores field definitions | Flags, Name, Signature |
MemberRef | References to members defined outside the current module | Class, Name, Signature |
CustomAttribute | Custom attribute data applied to metadata entities | Parent, Type, Value |
Sample Metadata Table Access via Reflection
// Display TypeDef table information for the current assembly
using System;
using System.Reflection;
class Program
{
static void Main()
{
var asm = Assembly.GetExecutingAssembly();
foreach (var type in asm.DefinedTypes)
{
Console.WriteLine($"Type: {type.FullName}");
Console.WriteLine($" Base Type: {type.BaseType}");
Console.WriteLine($" Methods:");
foreach (var m in type.DeclaredMethods)
Console.WriteLine($" {m.Name} ({m.Attributes})");
}
}
}
Understanding Table Relationships
Metadata tables are linked through indexes. For example, the MethodDef
table's ParamList
column points into the Param
table, defining the method's parameters. These relationships enable the CLR to navigate from a type to its members efficiently.