DEV Community

Roxeem
Roxeem

Posted on • Originally published at roxeem.com on

Incremental Source Generators in .NET

As .NET developers, we’re obsessed with performance and productivity. We build robust systems, but we often hit a wall with repetitive, boilerplate code. Whether it’s mapping DTOs, implementing INotifyPropertyChanged, or wiring up API clients, we find ourselves writing the same tedious patterns over and over.

For years, our primary tools for this “metaprogramming” were Reflection and T4 templates. Reflection is powerful, but it’s slow. It runs at runtime, it’s string-based, and it’s notoriously unfriendly to AOT (Ahead-of-Time) compilation and linkers. T4 templates are better, as they run before compilation, but they are “dumb.” They are just text files that don’t understand your code’s semantics.

What if we could have the best of both worlds? What if we could write code that runs during compilation, understands our code’s syntax and semantics, and generates new, high-performance C# files on the fly?

That’s exactly what .NET Source Generators do. They are a Roslyn compiler feature that lets you inspect a user’s code and add new source files to the compilation. In this post, I’ll show you what they are, why they’re a massive improvement over older techniques, and how to build a simple, high-performance generator from scratch.

Continue reading Incremental Source Generators in .NET on Roxeem.

Top comments (0)