Generating Code
So far in the book, we’ve looked at how powerful metaprogramming can be in .NET at runtime. Doing everything at runtime has the benefit of the flexibility to adapt to things that occur at runtime. The downside of doing this at runtime is that it has a performance impact. This is where the C# Roslyn compiler really shines. We had the capability to generate code in the past with commercial products such as PostSharp (https://www.postsharp.net/) or Intermediate Language (IL) weaving, using projects such as Fody (https://github.com/Fody/Fody) But with Roslyn, code generation has truly been democratized and made easy for anyone to do.
Personally, I’ve worked with all the techniques throughout the years, and finally, with Roslyn, I can realize a lot of the metaprogramming I like to do without sacrificing performance. And I can do so in a more consistent way than before.
The C# Roslyn compiler enables this by allowing developers to be part of its compilation...