Source generator is a fantastic feature of Roslyn in this .NET 5 release, by implementing ISourceGenerator you can get a result like that.
Source generator
ISourceGenerator