Paul Krill
Editor at Large

C# Source Generators to boost performance in .NET 5

news
May 5, 20202 mins

A new C# compiler feature that inspects code and generates additional source files promises to improve performance in a number of scenarios

gears 142263619
Credit: Thinkstock

Microsoft has introduced a preview of a C# compiler capability called Source Generators that can inspect a program and generate source files that can be added to a compilation. Microsoft says Source Generators can improve performance in a number of scenarios.

Introduced April 29, a Source Generator is a piece of code (a .NET Standard 2.0 assembly) that runs during compilation and can inspect a program to produce additional files that are compiled together with the rest of the code.

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.

Microsoft said user code can be inspected along with rich metadata that the compiler builds up during compilation, with C# code emitted back into the same compilation based on the data analyzed. Source Generators are akin to the Roslyn Analyzers that inspect C# or Visual Basic code, serving as analyzers that can emit C# source code.  

Source Generators are to be shipped as part of C# 9. The company has made a Source Generators Cookbook and a Source Generators design document available to help developers get started.

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.

To access Source Generators, developers must install the latest .NET 5 preview and the latest Visual Studio preview

Paul Krill

Paul Krill is editor at large at InfoWorld. Paul has been covering computer technology as a news and feature reporter for more than 35 years, including 30 years at InfoWorld. He has specialized in coverage of software development tools and technologies since the 1990s, and he continues to lead InfoWorld’s news coverage of software development platforms including Java and .NET and programming languages including JavaScript, TypeScript, PHP, Python, Ruby, Rust, and Go. Long trusted as a reporter who prioritizes accuracy, integrity, and the best interests of readers, Paul is sought out by technology companies and industry organizations who want to reach InfoWorld’s audience of software developers and other information technology professionals. Paul has won a “Best Technology News Coverage” award from IDG.

More from this author