Search⌘ K
AI Features

Reflection and Source Generators

Explore the concepts of reflection and source generators in .NET, discovering how reflection inspects code at runtime while source generators create code at compile-time. Understand when to use each method to optimize application performance, flexibility, and compatibility across different environments.

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 ...