Code-Generierung in .NET Core: Roslyn Source Generators
In diesem Webinar lernst Du, wie ein Roslyn Source Generator implementiert wird. Zuerst sehen wir uns einen Roslyn Source Generator in Aktion an und werden ihn anschließend gemeinsam implementieren.
in action • Motivation • Code generation today • Benefits and limitations • Some ideas: source gen is not just for complex libraries and frameworks • Implementation of a new Roslyn Source Generator Webinar is based on blog post: https://www.thinktecture.com/en/net/roslyn-source-generators-introduction/ Agenda
with • T4-Templates • CodeDOM / IL Generator • Expressions (Func<T>/Action<T>) • Roslyn Source Generators Code generation today development time compile time run time
new files out of static source files, like XML/CSV/JSON, on demand • Prominent example: Generation of entities in Entity Framework (non-Core) 6 • Pros: • Code generation at development time : easy to review • Generated types can be used during development directly • Cons: • No built-in means to analyze the current .NET code • No means to analyze the current state of the application • Doesn‘t react to changes in source files automatically • Virtually bound to MS Visual Studio. JetBrains Rider requires some hacks and workarounds to work properly. Code generation today development time compile time run time T4
• Generation and compilation of C# code or IL at runtime • Pros: • Analysis of types and current state of the application possible • No performance penalties when using the generated types, unless reflection is used • Cons: • CodeDOM: C# code compilation at runtime is expensive • IL Generator: IL is difficult to write because very cryptic • Difficult review of the generated code, due to code generation at runtime • Generated types cannot be used during development directly because they don’t exist yet Code generation today development time compile time run time CodeDOM/IL Gen
and compilation of lambda expressions at runtime • Pros: • Analysis of types and current state of the application possible • No performance penalties when using the compiled delegate (besides being a delegate) • The compiled delegates usually are typed and can be used during development directly • Cons: • Compilation of the expression is not for free • Generation of whole types is not possible, only delegates (i.e. methods) Code generation today development time compile time run time Expressions
C# compiler feature • Requires .NET 5 SDK or higher for usage (i.e. for compilation) • Main reasons: • Performance: lowers the need of (expensive) reflection • Better API and development experience Code generation today development time compile time run time Roslyn (IDE) Roslyn (explicit compilation)
Continuous code generation at development time (IDE) and during “explicit” compilation • Pros: • Analysis of C# code possible (similar to Reflection) • No performance penalties at runtime • The generated types can be reviewed and used immediately • Cons: • Full power with supported IDEs only (like VS, Rider) • The Source Generators don’t see each other => “Source Gen A” cannot work on result of “Source Gen B” Code generation today development time compile time run time Roslyn (IDE) Roslyn (explicit compilation)