Advertisement
Beginner C#, Attributes & Reflection

What is reflection, and what basic information can you retrieve from a Type object at runtime?

Short answer

Reflection lets you inspect metadata about types, methods, properties, and assemblies AT RUNTIME, even for types you don't have compile-time knowledge of — a Type object (obtained via typeof() or GetType()) exposes its name, properties, methods, constructors, and more.

Save C# mock interview
Advertisement
AModel answer
Reflection lets you inspect metadata about types, methods, properties, and assemblies AT RUNTIME, even for types you don't have compile-time knowledge of — a Type object (obtained via typeof() or GetType()) exposes its name, properties, methods, constructors, and more.
Type type = typeof(string);
Console.WriteLine(type.Name);        // "String"
Console.WriteLine(type.Namespace);   // "System"
var methods = type.GetMethods();     // array of all public methods
Real-world example
Building a generic object inspector/debugger tool that can display any object's properties without knowing its type ahead of time.
Likely follow-ups
  • What's the difference between typeof(SomeType) and instance.GetType()?
Related topics