C# EXPERTChapter 6 · C# Expert
Reflection and custom attributes
Use Reflection via System.Reflection to inspect metadata at runtime. Retrieve types, read custom attributes, and instantiate objects or call methods dynamically.
Worked example
using System;
using System.Reflection;
class Program {
static void Main() {
Type t = typeof(Program);
foreach (var method in t.GetMethods()) {
Console.WriteLine(method.Name);
}
}
}How it reads
- typeof(T) retrieves the metadata definition for type T
- Reflection enables dependency injection, serialization, and dynamic routing plugins

Cloud tip: Reflection is highly versatile but incurs a performance cost; cache type lookups where possible.


