$30 off During Our Annual Pro Sale. View Details »

Incremental Roslyn Source Generators in .NET 6

Incremental Roslyn Source Generators in .NET 6

Pawel Gerr

May 27, 2022
Tweet

More Decks by Pawel Gerr

Other Decks in Programming

Transcript

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

    View Slide

  2. Code-Generierung in .NET 6
    Incremental 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

    View Slide

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

    View Slide

  4. Code-Generierung in .NET 6
    Incremental 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

    View Slide

  5. Code-Generierung in .NET 6
    Incremental 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

    View Slide

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

    View Slide

  7. Code-Generierung in .NET 6
    Incremental 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

    View Slide

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

    View Slide

  9. Code-Generierung in .NET 6
    Incremental 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

    View Slide

  10. Code-Generierung in .NET 6
    Incremental 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

    View Slide

  11. Code-Generierung in .NET 6
    Incremental Roslyn Source Generators
    (Incremental) Roslyn 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)

    View Slide

  12. Code-Generierung in .NET 6
    Incremental Roslyn Source Generators
    (Incremental) 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)

    View Slide

  13. Code-Generierung in .NET 6
    Incremental 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

    View Slide

  14. Code-Generierung in .NET 6
    Incremental 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

    View Slide

  15. Code-Generierung in .NET 6
    Incremental 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

    View Slide

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

    View Slide

  17. Code-Generierung in .NET 6
    Incremental Roslyn Source Generators
    Learnings
    • Delivers best performance in comparison to reflection
    • Improve the development experience with Roslyn Analyzers and Roslyn Code Fixes
    • Goals: faster application development, 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 Posts: https://www.thinktecture.com/thinktects/pawel-gerr/
    • Smart Enums: https://github.com/PawelGerr/Thinktecture.Runtime.Extensions
    Ask me
    @pawelgerr
    [email protected]

    View Slide