Reflection and Source Generators

Understand the conceptual differences between runtime reflection and compile-time source generators to choose the right metadata strategy for performance and modern .NET compatibility.

In the previous lessons, we used reflection to inspect code and invoke members while the application was already running. While reflection is incredibly powerful, it comes with a runtime tax because the computer must stop to look up metadata during execution. Modern .NET development introduces an alternative called source generators.

What are source generators?

A source generator is a piece of code that runs during the compilation phase. It inspects your code just like reflection does, but instead of acting on it at runtime, it generates additional C# source code that gets compiled along with the rest of your project.

Key differences

The following table compares how these two technologies handle metadata: