What is the cost of .NET reflection?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
Reflection in .NET is expensive compared with direct, statically compiled access, but the real question is where that cost shows up. One-time metadata inspection is often acceptable, while repeated reflective invocation inside hot loops can become a genuine performance problem.
What Reflection Actually Costs
Reflection does work the runtime cannot optimize as aggressively as normal code:
- metadata lookup
- name-based member discovery
- argument boxing and unboxing in some cases
- runtime access checks
- indirect invocation through APIs such as
MethodInfo.Invoke
That means this kind of code is much slower than a direct call:
The most expensive part is usually repeated dynamic invocation, not simply obtaining a Type once.
Cheap Enough Versus Too Expensive
Reflection is usually fine when:
- scanning assemblies at startup
- loading plugins occasionally
- building serializers or mappers once and caching the result
Reflection becomes risky when:
- used per request in a hot path
- used per item in a large loop
- used repeatedly to discover the same members without caching
That is why many frameworks use reflection only during setup, then compile delegates or cache metadata for steady-state execution.
Example Benchmark Shape
The exact numbers depend on machine and runtime, but the reflective loop will be dramatically slower. That does not mean reflection is "bad." It means you should avoid paying that price repeatedly when the member information is stable.
The Standard Mitigation: Cache and Compile
If you must discover members dynamically, do it once and cache the result. For frequent invocation, create a delegate when possible:
This keeps the flexible discovery step while avoiding repeated MethodInfo.Invoke overhead.
The same idea applies to property access and constructor activation. If a dynamic path is used frequently, move the expensive reflective discovery to startup or first use, then keep a cached executable path for steady-state calls.
Reflection Cost Is Not Only Speed
There is also a maintainability cost:
- compile-time checks are weaker
- errors shift to runtime
- code is harder to follow
So even if performance is acceptable, reflective code should still justify its complexity.
Common Pitfalls
The biggest mistake is benchmarking reflection once, seeing it is slower, and concluding it must never be used. Startup-time or rarely executed reflection is often perfectly reasonable.
Another mistake is doing repeated GetProperty, GetMethod, or Invoke calls inside tight loops without caching.
A third issue is using reflection when generics, interfaces, or delegates would solve the problem more simply and safely.
Summary
- Reflection is slower than direct code, especially for repeated invocation.
- One-time metadata discovery is often acceptable.
- Hot-path
MethodInfo.Invokeand repeated lookup are where the cost becomes significant. - Cache metadata and prefer compiled delegates when dynamic invocation is frequent.
- The cost of reflection includes maintainability and runtime error risk, not just CPU time.

