C# Interview Question #89

What is reflection in C#?

Advanced C# Language Features Mid-Level Intermediate

Quick Interview Answer

Reflection allows code to inspect metadata about assemblies, types, properties, methods, attributes, constructors, and other members at runtime.

Detailed Explanation

Reflection allows code to inspect metadata about assemblies, types, properties, methods, attributes, constructors, and other members at runtime.

It is used by frameworks for serialization, dependency injection, mapping, testing, plugins, and convention-based behavior.

Reflection is powerful but generally slower and less compile-time-safe than direct code. It should be used when dynamic runtime discovery is genuinely required rather than as a replacement for normal strongly typed design.

Code Example

Type type = typeof(Product);

Console.WriteLine(type.Name);

foreach (var property in type.GetProperties())
{
    Console.WriteLine(property.Name);
}