Skip to main content

C# Source Generators - a key to metaprogramming !!

C# Source generators - finally you are real !!

Guys, if you are interested in compile time metaprogramming then this blog is a good place to land.

Source code generator is a much awaited feature and now a reality in .Net 5 preview version. If you are not aware of what is source generator then don't worry, I expect most of the developers at the time when this blog is published are not aware of this feature. Take a deep breadth and relax!!

Source code generators are the new feature of C# compiler or you can say the compiler capability that can inspect a program at the compile time and generate new source files that can be added to source code while compiling the code. This functionality is a piece of code in .Net Standard 2.0 assembly. So C# developers now you can inspect the user code and generate C# source files that can be added to a compilation. Imagine the new possibilities at your door with this concept and we will discuss a few use cases in a while.

Source code generator design goals

  • Generators produce one or more strings that represent C# source code to be added to the compilation.
  • Explicitly additive only. Generators can add new source code to a compilation but may not modify existing user code.
  • Can produce diagnostics. When unable to generate source, the generator can inform the user of the problem.
  • May access additional files, that is, non-C# source texts.
  • Run un-ordered, each generator will see the same input compilation, with no access to files created by other source generators.
  • A user specifies the generators to run via list of assemblies, much like analyzers.

Source Generators let C# developers do the following

  • Retrieve a compilation representing all user code being compiled. This object can be inspected and developers can write code that works with the syntax and semantic models for code being compiled, like with analyzers.
  • Generate C# source files to be added to a compilation object during compilation, with additional source code provided as input while code is being compiled.
When combined, these two things are what make Source Generators so useful. You can inspect user code with all of the rich metadata that the compiler builds up during compilation, then emit C# code back into the same compilation that is based on the data you’ve analyzed! If you’re familiar with Roslyn Analyzers, you can think of Source Generators as analyzers that can emit C# source code.

Scenarios that benefit from Source Generators include

  • To perform runtime reflection. By analyzing code when an app starts up, for example, a controller discovery phase could happen at compile time, resulting in faster startup times.
  • To obviate the use of “stringly typed” APIs, such as how ASP.NET Core routing between controllers and Razor pages works. Routing could be strongly typed and the necessary strings generated as a compile-time detail.
  • To help remove barriers to linker-based and ahead-of-time compilation optimizations.
If we talk about the runtime reflection, ASP.NET Core uses reflection when your web service first runs to discover constructs you’ve defined so that it can “wire up” things like controllers and razor pages. Although this enables you to write straightforward code with powerful abstractions, it comes with a performance penalty at runtime: when your web service or app first starts up, it cannot accept any requests until all the runtime reflection code that discovers information about your code is finished running! Although this performance penalty is not enormous, it is somewhat of a fixed cost that you cannot improve yourself in your own app.

With a Source Generator, the controller discovery phase of startup could instead happen at compile time by analyzing your source code and emitting the code it needs to “wire up” your app. This could result in some faster startup times, since an action happening at runtime today could get pushed into compile time.

When we talked about the stringly typed APIs, Source Generators can offer is obviating the use of some “stringly-typed” APIs, such as how ASP.NET Core routing between controllers and razor pages work. With a Source Generator, routing can be strongly typed with the necessary strings being generated as a compile-time detail. This would reduce the amount of times a mistyped string literal leads to a request not hitting the correct controller.

Will try to add the real time examples soon! Excited to try my hands on this feature.

Comments

  1. 1xbet korean | Legalbet.co.kr
    1xbet korean. 1xbet korean No deposit bonus kerran sebagai dengan 메리트카지노 uang asli. Min Deposit, 1.000. Deposits, 20%. Minimum deposit, 100.000. ボンズ カジノ Bonus, 100.000. Bonus. No

    ReplyDelete
  2. Silly Game - Topcleo.app
    Silly Game · The latest Silly Game game news & reviews. Play the biggest collections with the latest 소울 카지노 Silly Game ad 우리카지노 downloads: www.sillygame.blogspot.com. Rating: 1 · ‎14 reviews

    ReplyDelete

Post a Comment

Popular posts from this blog

Window authentication with Identity Server 4 - Part 1 (hybrid client)

Before reading this post, you must be aware of basics of identity server and oauth terms. We will not delve into the basics of identity server here. Setting up identity server 4 Setup a .Net Core 3.x project and add the nuget package reference for Identity server 4. Important point here is to have a project with a flexibility to add new views/pages. Once we add the nuget reference of Identity server 4, we will add a few lines of code in ConfigureServices method of our Startup.cs file Here is a look at our Config.cs class This way we have added a client with a custom grant type "iwa" representing integrated window authentication. This will be used in part 2 when we will protect our API resources for a SPA. Window authentication with hybrid flow i.e. MVC client Next we will create a MVC project (you can create any web project). We need to ensure that the client parameters used in this project should be same as registered in Identity Server setup. Here is t