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
had contact with • T4-Templates • CodeDOM / IL Generator • Expressions (Func<T>/Action<T>) • Roslyn Source Generators Code generation today development time compile time run time
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
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
• 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
Source Generators • C# compiler feature • Requires .NET 5 SDK for implementation of Source Generators, or .NET 6 SDK for Incremental Source Generators • 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)
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)
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
ideas (3) Make the types JSON-serializable without explicit configuration of a JsonConverter Source Gen is not just for complex libraries and frameworks