✅ Use Roslyn to auto-generate overloads of method
I'm working on some code and the amount of overloads I'm having to write is very quickly getting out of hand. I have a bunch of related functions which are overloaded based on the number of parameters in a provided
Action
. For example, one of the longer methods looks like:
I came across a blog post here about source generators in .NET 7, and was curious if I could use similar techniques here to totally omit the need to hand-write all these overloads and leave it to a source generator. However, I've never used anything related to source generators or Roslyn before and would like some guidance. Any help would be appreciated.
(Tagging both intermediate and advanced since I'm not sure what this is considered-- someone let me know).NET Source Generators with .NET 7
Exploring .NET source generators to reduce boilerplate code in everyday code!
3 Replies
For context, there are overloads of this method for anywhere between 1 and 13 generic parameters, and there's about 3-4 other related methods which need an equal amount of overloads, and I am getting overwhelmed by the unmaintainability of all of this.
you should be using incremental generators
ISourceGenerator is deprecated, as it has a number of unfixable design issues
#roslyn is your friend
gotcha, thanks
I decided T4 templates get the job done.