Why is no-one using Roslyn token-based code generation with Source Generators?

This page summarizes the projects mentioned and recommended in the original post on reddit.com/r/dotnet

Our great sponsors
  • Scout APM - Less time debugging, more time building
  • SonarLint - Clean code begins in your IDE with SonarLint
  • SaaSHub - Software Alternatives and Reviews
  • Roslyn

    The Roslyn .NET compiler provides C# and Visual Basic languages with rich code analysis APIs.

    I'm planning to rewrite all of our company's old T4-based codegen tools into Source Generators. I've been looking at how other Source Generators are implemented and what surprised me is that everyone seems to assemble the output code manually as text with string builders or at best some simple templating engine (eg. Scriban). I expected that I would see a lot of projects assembling their output code from Roslyn tokens and then using Roslyn's formatter to produce the actual code that can be emitted into the output file(s), like this article demonstrates. No-one seem to be doing that though. Why?

  • csharp-source-generators

    A list of C# Source Generators (not necessarily awesome) and associated resources: articles, talks, demos.

    I'm planning to rewrite all of our company's old T4-based codegen tools into Source Generators. I've been looking at how other Source Generators are implemented and what surprised me is that everyone seems to assemble the output code manually as text with string builders or at best some simple templating engine (eg. Scriban). I expected that I would see a lot of projects assembling their output code from Roslyn tokens and then using Roslyn's formatter to produce the actual code that can be emitted into the output file(s), like this article demonstrates. No-one seem to be doing that though. Why?

  • Scout APM

    Less time debugging, more time building. Scout APM allows you to find and fix performance issues with no hassle. Now with error monitoring and external services monitoring, Scout is a developer's best friend when it comes to application development.

  • jab

    C# Source Generator based dependency injection container implementation.

    Having the same issues trying to get into source generators. I don't see too many opportunities to include it in my day-to-day development, or at least with the examples I've seen thus far. The exception being the dependency-injection based ones like https://github.com/pakrym/jab

  • dotnet

    .NET Community Toolkit is a collection of helpers and APIs that work for all .NET developers and are agnostic of any specific UI platform. The toolkit is maintained and published by Microsoft, and part of the .NET Foundation. (by CommunityToolkit)

    I'm using Roslyn APIs in all the generators in the Microsoft MVVM Toolkit, honestly yes the learning curve is a bit steep at first but they grew on me and now I just love them. I feel like they make it super easy to keep the code modular and to change things over time. Plus you can choose how granular you want the generation to be as well. I would generally recommend this approach, to be honest 🙂

NOTE: The number of mentions on this list indicates mentions on common posts plus user suggested alternatives. Hence, a higher number means a more popular project.

Suggest a related project

Related posts