Slide 1

Slide 1 text

Code-Generierung in .NET Roslyn Source Generators Pawel Gerr @pawelgerr [email protected]

Slide 2

Slide 2 text

Code-Generierung in .NET Roslyn Source Generators • Roslyn Source Generator 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

Slide 3

Slide 3 text

Code-Generierung in .NET Roslyn Source Generators DEMO: Roslyn Source Generator in action

Slide 4

Slide 4 text

Code-Generierung in .NET Roslyn Source Generators … I had contact with • T4-Templates • CodeDOM / IL Generator • Expressions (Func/Action) • Roslyn Source Generators Code generation today development time compile time run time

Slide 5

Slide 5 text

Code-Generierung in .NET Roslyn Source Generators T4-Templates • Generation of 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

Slide 6

Slide 6 text

Code-Generierung in .NET Roslyn Source Generators T4-Templates Like Razor syntax, but different Code generation today development time compile time run time T4

Slide 7

Slide 7 text

Code-Generierung in .NET Roslyn Source Generators CodeDOM / IL Generator • 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

Slide 8

Slide 8 text

Code-Generierung in .NET Roslyn Source Generators CodeDOM / IL Generator Emitting IL Code generation today development time compile time run time CodeDOM/IL Gen

Slide 9

Slide 9 text

Code-Generierung in .NET Roslyn Source Generators Expressions (Func/Action) • Generation 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

Slide 10

Slide 10 text

Code-Generierung in .NET Roslyn Source Generators Expressions (Func/Action) • Kind of a mixture out of IL Generator and Reflection Code generation today development time compile time run time Expressions

Slide 11

Slide 11 text

Code-Generierung in .NET Roslyn Source Generators Roslyn Source Generators • 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)

Slide 12

Slide 12 text

Code-Generierung in .NET Roslyn Source Generators Roslyn Source Generators • 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)

Slide 13

Slide 13 text

Code-Generierung in .NET Roslyn Source Generators Just some ideas (1) Generation of constructors or methods, like ShallowClone, in all or some specific classes Source Gen is not just for complex libraries and frameworks

Slide 14

Slide 14 text

Code-Generierung in .NET Roslyn Source Generators Just some ideas (2) Generation of translations keys out of text files, like JSON Source Gen is not just for complex libraries and frameworks

Slide 15

Slide 15 text

Code-Generierung in .NET Roslyn Source Generators Just some ideas (3) Make the types JSON-serializable without explicit configuration of a JsonConverter Source Gen is not just for complex libraries and frameworks

Slide 16

Slide 16 text

Code-Generierung in .NET Roslyn Source Generators Implementation of a new Roslyn Source Generator

Slide 17

Slide 17 text

Code-Generierung in .NET Roslyn Source Generators Learnings • Delivers best performance in comparison to reflection • Improve the development experience with Roslyn Analyzers and Roslyn Code Fixes • Goals: faster applications, better work-life-balance ☺ Resources • MSDN: https://docs.microsoft.com/en-us/dotnet/csharp/roslyn-sdk/source-generators-overview • Json Serializer: https://devblogs.microsoft.com/dotnet/try-the-new-system-text-json-source-generator/ • Blog Post: https://www.thinktecture.com/en/net/roslyn-source-generators-introduction/ • Smart Enums: https://github.com/PawelGerr/Thinktecture.Runtime.Extensions Ask me @pawelgerr [email protected]