April 29th, 2020

Introducing C# Source Generators

Phillip Carter
Program Manager

We’re pleased to introduce the first preview of Source Generators, a new C# compiler feature that lets C# developers inspect user code and generate new C# source files that can be added to a compilation. This is done via a new kind of component that we’re calling a Source Generator.

To get started with Source Generators, you’ll need to install the latest .NET 5 preview and the latest Visual Studio preview. Note: to build a source generator, you currently require Visual Studio. This will change in the next .NET 5 preview.

What is a Source Generator?

Unless you’ve been closely following every prototype and proposal related to the C# language and compiler, then there’s a good chance you’re asking, “What is a Source Generator” right now. A Source Generator is a piece of code that runs during compilation and can inspect your program to produce additional files that are compiled together with the rest of your code.

A Source Generator is a new kind of component that C# developers can write that lets you do two major things:

  1. Retrieve a Compilation object that represents all user code that is being compiled. This object can be inspected and you can write code that works with the syntax and semantic models for the code being compiled, just like with analyzers today.
  2. Generate C# source files that can be added to a Compilation object during the course of compilation. In other words, you can provide additional source code as input to a compilation while the 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.

Source generators run as a phase of compilation visualized below:

Image Picture1

A Source Generator is a .NET Standard 2.0 assembly that is loaded by the compiler along with any analyzers. It is usable in environments where .NET Standard components can be loaded and run.

Now that you know what a Source Generator is, let’s go through some of the scenarios they can improve.

Example scenarios that can benefit from Source Generators

The most important aspect of a Source Generator isn’t what it is, but what it can enable.

Today, there are three general approaches to inspecting user code and generating information or code based on that analysis used by technologies today: runtime reflection, IL weaving, and juggling MSBuild tasks. Source Generators can be an improvement over each approach.

Runtime reflection is a powerful technology that was added to .NET a long time ago. There are countless scenarios for using it. A very common scenario is to perform some analysis of user code when an app starts up and use that data to generate things.

For example, 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.

Source Generators can improve performance in ways that aren’t limited to reflection at runtime to discover types as well. Some scenarios involve calling the MSBuild C# task (called CSC) multiple times so they can inspect data from a compilation. As you might imagine, calling the compiler more than once affects the total time it takes to build your app! We’re investigating how Source Generators can be used to obviate the need for juggling MSBuild tasks like this, since Source generators don’t just offer some performance benefits, but also allows tools to operate at the right level of abstraction.

Another capability 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.

As we flesh out the API and experience writing Source Generators more, we anticipate more scenarios to become evident. We’re also planning on working with partner teams to help them adopt Source Generators if it improves their core scenarios.

Source Generators and Ahead of Time (AOT) Compilation

Another characteristic of Source Generators is that they can help remove major barriers to linker-based and AOT (ahead-of-time) compilation optimizations. Many frameworks and libraries make heavy use of reflection or reflection-emit, such as System.Text.Json, System.Text.RegularExpressions, and frameworks like ASP.NET Core and WPF that discover and/or emit types from user code at runtime.

We’ve also identified that many of the top NuGet packages people make heavy use of reflection to discover types at runtime. Incorporating these packages is essential for most .NET apps, so the “linkability” and ability for your code to make use of AOT compiler optimizations is greatly affected. We’re looking forward to working with our wonderful OSS community to see how these packages could use source generators and improve the overall .NET ecosystem.

Hello World, Source Generator edition

All the previous examples of source generators mentioned earlier are pretty complex. Let’s go through a very basic one to show some of the key pieces you’ll need to write your own Source Generator.

The goal is to let users who have installed this Source Generator always have access to a friendly “Hello World” message and all syntax trees available during compilation. They could invoke it like this:

Over time, we’ll make getting started a lot easier in tools with templates. For now, here’s how to do it manually:

1. Create a .NET Standard library project that looks like this:

The key pieces of this is that the project can generate a NuGet package and it depends on the bits that enable Source Generators.

2. Modify or create a C# file that specifies your own Source Generator like so:

You’ll need to apply the Microsoft.CodeAnalysis.Generator attribute and implement the Microsoft.CodeAnalysis.ISourceGenerator interface.

3. Add generated source code to the compilation!

4. Add the source generator from a project as an analyzer and add preview to the LangVersion to the project file like this:

If you’ve written Roslyn Analyzers before, the local development experience should be similar.

When you write your code in Visual Studio, you’ll see that the Source Generator runs and the generated code is available to your project. You can now access it as if you had created it yourself:

Note: you will currently need to restart Visual Studio to see IntelliSense and get rid of errors with the early tooling experience

There are many more things you can do with Source Generators than just something simple like this:

  • Automatically implement interfaces for classes with an attribute attached to them, such as INotifyPropertyChanged
  • Generate settings files based on data inspected from a SourceGeneratorContext
  • Serialize values from classes into JSON strings
  • etc.

The Source Generators Cookbook goes over some of these examples with some recommended approaches to solving them.

Additionally, we have a set of samples available on GitHub that you can try on your own.

As mentioned earlier, we’re working on making the experience authoring and using Source Generators better in tooling, such as adding templates, allowing for seamless IntelliSense and navigation, debugging, and improving responsiveness and performance in Visual Studio when generating source files.

Source Generators are in preview

As mentioned earlier in this post, this is the first preview of Source Generators. The intention of releasing this first preview is to let library authors try out the feature and give us feedback on what’s missing and what needs to change. From preview to preview, there may be changes in the API and characteristics of source generators. We intend on shipping Source Generators as GA with C# 9, and sometime later this year we intend on stabilizing the API and features it provides.

Calling all C# library developers: try it out!

If you own a .NET library written in C#, now is a great time to evaluate Source Generators and see if they’re a good fit. There’s a good chance that if your library makes heavy use of reflection, you’ll benefit in some way.

To help with that, we recommend reading the following docs:

Give us your feedback and let us know what you need! We’d love to learn more about how you think Source Generators could improve your code, and what you feel is missing or needs changing.

What’s next for Source Generators

This first preview is is exactly that: a first preview. There is a basic editing experience in Visual Studio, but it is not what we would consider “1.0 quality” right now. We may explore a few different designs over time before we commit to a particular one. One of the biggest areas of focus between now and the .NET 5 release will be improving the editing experience for Source Generators. Additionally, we expect to modify the API to accommodate feedback from partner teams and our OSS community.

Additionally, we’ll ensure a good experience for how Source Generators are distributed. We’re currently designing them to be very similar to Analyzers that can be shipped alongside a package. They currently use the Analyzer infrastructure to handle configuration in editor tooling.

FAQ

Below is a list of questions we anticipate some people might have. We’ll update this list with more questions as they come.

How do Source Generators compare to other metaprogramming features like macros or compiler plugins?

Source Generators are a form of metaprogramming, so it’s natural to compare them to similar features in other languages like macros. The key difference is that Source Generators don’t allow you _rewrite_ user code. We view this limitation as a significant benefit, since it keeps user code predictable with respect to what it actually does at runtime. We recognize that rewriting user code is a very powerful feature, but we’re unlikely to enable Source Generators to do that.

How do Source Generators compare with Type Providers in F#?

If you’re an F# programmer (or familiar with the language), then you might have heard of Type Providers. Source Generators were inspired in part by Type Providers, but there are several differences that make them distinct. The main difference is that Type Providers are a part of the F# language proper and emit types, properties, and methods in-memory based on an external source. Source Generators are a compiler feature that analyzes C# source code, optionally with other files, emits C# source code to include back into a compilation.

Should I delete all my reflection code?

No! Reflection is an incredibly useful tool to use. Reflection does present some performance and “linkability” challenges that can be solvable with Source Generators in some scenarios. We recommend carefully evaluating if Source Generators fit your scenario.

How are Source Generators this different from analyzers?

Source Generators are similar to analyzers, since both are compiler features that let you plug into a compilation. The key difference is that analyzers ultimately emit diagnostics that can be used to associate with a code fix. Source Generators ultimately emit C# source code that is added to a compilation. There are several other differences discussed in the design document.

Can I modify/rewrite existing code with a Source Generator?

No. As mentioned earlier, Source Generators do not allow you to rewrite user source code. We do not intend on allowing them to this. They can only augment a compilation by adding C# source files to it.

When will Source Generators be out of preview?

We intend on shipping Source Generators with C# 9. However, in the event that they aren’t ready in time, we’ll keep them in preview and ensure that users need to opt in to use them.

Can I change the TFM in a Source Generator?

Technically, yes. Source Generators are .NET Standard 2.0 components, and like any project you can change the TFM. However, they only support being loaded into consuming projects as .NET Standard 2.0 components today.

Will Source Generators come to Visual Basic or F#?

Source Generators are currently a C# only feature. Because this is the first preview, there are many things that can change between now and the released version. We do not intend on adding Source Generators to Visual Basic at this time. If you’re an F# developer and want to see this feature added, please search the suggestions or file a new one in the F# language suggestion repository.

Do Source Generators introduce compatibility concerns for libraries?

This depends on how libraries are authored. Since VB and F# currently don’t support Source Generators, library authors should avoid designing their features such that they require a Source Generator. Ideally, features have fallbacks to runtime reflection and/or reflection emit. This is something that library authors will need to careful consider before adopting Source Generators. We expect most library authors will use Source Generators to augment – rather than replace – current experiences for C# developers.

Why do I not get IntelliSense for generated code? Why does Visual Studio say there’s an error even though it builds?

You will need to restart Visual Studio after building the source generator to make errors go away and IntelliSense appear for generated source code. After you do that, things will work. Currently, Visual Studio integration is very early on. This current behavior will change in the future so that you don’t need to restart Visual Studio.

Can I debug or navigate to generated source in Visual Studio?

Eventually, we’ll support navigation and debugging of generated source in Visual Studio. It is not yet supported in this early preview stage.

How do I ship my own Source Generator?

Source Generators can be shipped as NuGet packages, just like Analyzers today. In fact, they use the same “plumbing” as Analyzers. If you’ve ever shipped an Analyzer, then you can easily ship a Source Generator.

Will there be Microsoft-authored Source Generators?

Eventually, yes. But this is still the first preview of the technology, and a lot of things may need to change to accommodate various scenarios. There is currently no timeline for when Microsoft-authored Source Generators are available.

Why do I need to use the Preview LangVersion to consume a Source Generator?

Although Source Generators are not technically a C# language feature, they are in preview. Rather than introduce a new setting just for Source Generators, we decided it would be easier to just use the existing switch that enables preview language features for the C# compiler.

 

Cheers, and happy source generation!

Category
.NET

Author

Phillip Carter
Program Manager

Phillip is a PM on the .NET team, focusing on the F# language, F# documentation, F# tooling, and project system tooling. He wishes he had more time to code, but that doesn't stop him from having fun with people on GitHub. He loves functional programming and language-related tooling, and is always available to chat about wild and wacky ways to make programming more enjoyable.

108 comments

Discussion is closed. Login to edit/delete existing comments.

  • Alexandre Brina

    This is a very nice feature but source code written in strings doesn’t look right. Please let code be written as code, with all the IDE support like syntax coloring, code completion, debugging, etc. I mean, it can in fact be strings, allowing interpolation, etc, but written in a regular file, template-like, that let us see the code right away “as code” instead of string-building.

  • Antão Almada

    I ported my source generator project from using Uno.SourceGeneration to using the new C# Source Generators. It was actually very simple as these are very similar.

    It's an internal generator that automatically generates lots of overloads to my types so that value types are not boxed. You can find the code at https://github.com/NetFabric/NetFabric.Hyperlinq/tree/cb76d29c8d9b91935079fa4d226e393b01b1f03f/NetFabric.Hyperlinq.SourceGenerator

    I'm planning to add a public generator to generate binding methods.

    Read more
  • Jinming Mu

    This is a very promising feature. but so far It is similar with T4 if it could not manipulate the exiting code.
    and It is unnecessary if just for boot faster. AOP it will be one of its major features but not all.

    • David Lewis

      T4 templates don't have access to your programs structure at compile time. even when running from the same assembly. Source generators do have access to that structure and are brought in as a library rather than copy pasting into your source code. since source generators allow for compile time introspection and code generation there are opportunities to do things like IOC containers with zero runtime reflection overhead and I can see...

      Read more
  • Pierre Picard

    Hi,
    This is an interesting feature indeed, but there was another way to do the same thing. Let's consider the C++ concept of constexpr. If there was a similar feature in C# like "compile time blocks" which would contain code executed at compile time, it would allow to do the same thing as Source Generators for most cases. I understand that it would be much more work for the team working on the C# compiler,...

    Read more
  • michael Lang

    A long time ago, when .NET 1.0 was the new kid on the block, Code Generators (not to be confused with Source Generators) were all the rage. For a long time, for many reasons, I had no interest them. I only became sold on Code Generators the day MS introduced partial classes in .NET 2.0, however just when I was finally on board, they fell out of favour. With .NET 2.0 they also...

    Read more
  • Dr. Chris of Rain

    I ported a serializer library from a project of mine to use source generators. Here are some of my thoughts:

    All that being said, I think this is a pretty cool feature and I am definitively looking forward to its official release. Keep up the good work!

    Read more
    • Mourad CHIBANE

      For debugging you can simply add :

      System.Diagnostics.Debugger.Launch();

      and at compile time you will be able to attach your debugger.

  • Scott Holodak

    By any chance can the generator live in a netstandard2.1 library? I need to consume a library that uses IAsyncEnumerable with await foreach‘s.

  • Frederic Forjan

    Did you consider this as a way to rewrite resources (resx) in .net and avoid having the C# added under source control ?
    i can see this to generate the resources.generated.cs on the fly and avoid to ‘manage’ it ?

  • Kalle Niemitalo

    On Windows, does the C# compiler ask Antimalware Scan Interface (AMSI) to scan the generated source code, like PowerShell does (SecuritySupport.cs)? Antimalware software can install a file system minifilter to scan other source files when they are read or written, but that will not work with source code that is generated by a source generator and then used by the C# compiler without ever being written to a file.

    Read more
  • circles-arrows.com

    OMG! FINALLY!!!!!

    This is great for injecting code from (xml / json) metadata files (even code like: #error Metadata file abc has problem xyz, mWahahaha)
    And an extension method generator for “default interface methods”, so we get full mix-ins with c# 9 😀

    Thank you, thank you, thank you!