C# Interview Question #136
What is reflection, and what are its performance considerations?
Reflection, Dynamic & Runtime Senior Advanced
Quick Interview Answer
Reflection allows runtime inspection and interaction with metadata such as types, properties, methods, constructors, and attributes.
Detailed Explanation
Reflection allows runtime inspection and interaction with metadata such as types, properties, methods, constructors, and attributes.
It is useful for framework infrastructure, serializers, dependency injection containers, plugin systems, mapping, testing tools, and convention-based behavior.
Reflection is generally slower than direct strongly typed access and can make code harder for static analysis and AOT compilation. In performance-sensitive paths, discovered metadata can often be cached, or generated code and compiled delegates can be used instead.
Code Example
Type type = typeof(Product);
PropertyInfo[] properties =
type.GetProperties();
foreach (PropertyInfo property in properties)
{
Console.WriteLine(property.Name);
}