I want to briefly talk about an awesome feature that may come to C# sometime in future: Source Generators. It’s a huge topic and a very interesting one. I’ll try to summarize it nicely.
Source Generators are similar to how XAML partial code is generated. Based on markup, before build a tool generates partial class that contains Initialize() function called in constructor, fields named in XAML (that allows referencing them in the original partial) and in UWP the code backing x:Bind (compiled binding). Those files are then included in build and whole thing snaps into place. Now, that tool is included in build process by default when targeting XAML frameworks (e.g. UWP).
Source Generators aim to make code generation a part of compiler pipeline, similar to analyzers in Roslyn. A generator would get syntax/semantic model of compiled documents and _add new documents _to the process. These documents would mostly contain partial classes which would contain boilerplate code. The very first example provided each time this topic comes up is of course INotifyPropertyChanged. Just mark a class with some attribute and whole thing gets hidden and written automagically. Just like every dev loves it!
- Champion “Replace/original and code generation extensions
- Discussion: Code Generator Catalog
- Spec (Prototype)