Introducing Source Generators in .NET 5 for Fast C# Code

Your compiler has been secretly capable of writing code for you this whole time. Source Generators are a Roslyn feature that run during compilation and inject code directly into your build - no reflection, no runtime overhead, no excuses. You write a partial class, the generator writes the other half. By the time your app runs, it's all just plain compiled C#. Spooky fast. Microsoft shipped them in .NET 5 (2020), mostly to fix the "why is JSON serialization slow" complaints. Spoiler: it was reflection. It's always reflection. --- // You write this: public partial class PizzaOrder { ... } // The generator writes this at compile time: public string Describe() => $"{Quantity}x {Topping} - your arteries called"; --- Try it yourself (no setup required): https://lnkd.in/emee3pA8 #dotnet #csharp #programming #roslyn

  • No alternative text description for this image

To view or add a comment, sign in

Explore content categories