Roslyn Source Generators: Configuration – Part 10

In this article we will see how to pass configuration parameters to a Roslyn Source Generator to control the output or enable/disable features.

In diesem Artikel:

pg
Pawel Gerr ist Architekt und Consultant bei Thinktecture. Er hat sich auf .NET Core Backends spezialisiert und kennt Entity Framework von vorne bis hinten.

More information about the Smart Enums and the source code can be found on GitHub:

In the previous article we talked about reduction of the resource consumption by a Source Generator when running inside an IDE. Today’s topic is a basic one – the configuration of a Roslyn Source Generator.

Enable/Disable the Counter

Since part 4 of this series the Source Generator renders a counter into the output to see whether the code is regenerated or not. Although this counter is helpful for debugging purposes, still, it shouldn’t be in production-ready code.

				
					// <auto-generated />
#nullable enable

// generation counter: 4

using System.Collections.Generic;
using DemoLibrary;

namespace DemoConsoleApplication
{
   partial class ProductCategory
   {
       ...
				
			

Project-wide Configuration

Roslyn provides 2 alternatives to pass parameters to a Source Generator. One way is to use the .editorconfig, which is being use by Analyzers as well. The other option is to use MSBuild properties, which can be defined in a .csproj-file or Directory.Build.props.

I prefer the approach with MSBuild properties to change the parameters per project. The corresponding parameter name will be DemoSourceGenerator_Counter and the values to activate the feature are enable, enabled, and true. I’m using more than 1 value because I can’t remember myself which one it was to activate the feature. 🙂

To activate the feature in DemoConsoleApplication, we have to make 2 entries in .csproj-file. First, define DemoSourceGenerator_Counter in the PropertyGroup. Second, define CompilerVisibleProperty so our custom parameter is passed to the Source Generator.

We will take care of CompilerVisibleProperty later, so the developers don’t have to define this property when installing our NuGet package.

				
					<Project Sdk="Microsoft.NET.Sdk">

   <PropertyGroup>
      ...
      <DemoSourceGenerator_Counter>enable</DemoSourceGenerator_Counter>
   </PropertyGroup>

   <ItemGroup>
      <CompilerVisibleProperty Include="DemoSourceGenerator_Counter" />
   </ItemGroup>
				
			

Receive Parameters with AnalyzerConfigOptionsProvider

The configuration parameters can be received from the AnalyzerConfigOptionsProvider. The MSBuild properties are prefixed with build_property, i.e. the parameter name is build_property.DemoSourceGenerator_Counter.
				
					   public void Initialize(IncrementalGeneratorInitializationContext context) 
   { 
      var enumTypes = context.SyntaxProvider
                             ...;

 
      var options = GetGeneratorOptions(context); 

      ...
   } 
   
   protected static IncrementalValueProvider<GeneratorOptions> GetGeneratorOptions(
       IncrementalGeneratorInitializationContext context) 
   { 
      return context.AnalyzerConfigOptionsProvider
                    .Select((options, _) => 
                           { 
                              var counterEnabled = options.GlobalOptions 
                                  .TryGetValue("build_property.DemoSourceGenerator_Counter",
                                               out var counterEnabledValue)
                                  && IsFeatureEnabled(counterEnabledValue); 
 
                               return new GeneratorOptions(counterEnabled); 
                           }); 
   } 
 
   private static bool IsFeatureEnabled(string counterEnabledValue) 
   { 
      return StringComparer.OrdinalIgnoreCase.Equals("enable", counterEnabledValue) 
             || StringComparer.OrdinalIgnoreCase.Equals("enabled", counterEnabledValue) 
             || StringComparer.OrdinalIgnoreCase.Equals("true", counterEnabledValue); 
   }
				
			
The GeneratorOptions must be read-only and implement Equals and GetHashCode to prevent unnecessary re-generation of the code.
				
					namespace DemoSourceGenerator; 
 
public sealed class GeneratorOptions : IEquatable<GeneratorOptions> 
{ 
   public bool CounterEnabled { get; } 
 
   public GeneratorOptions(bool counterEnabled) 
   { 
      CounterEnabled = counterEnabled; 
   } 
 
   // Equals and GetHashCode
} 

				
			

Passing Options to Code Generation

The options must be combined with the existing pipeline to receive them in GenerateCode.

				
					   public void Initialize(IncrementalGeneratorInitializationContext context) 
   { 
      var enumTypes = context.SyntaxProvider 
                             ...; 
 
      var generators = context.GetMetadataReferencesProvider() 
                              ...;
 
      var options = GetGeneratorOptions(context); 
 
      context.RegisterSourceOutput(enumTypes.Combine(generators).Combine(options), GenerateCode); 
   }
   
   private static void GenerateCode( 
      SourceProductionContext context, 
      ((DemoEnumInfo, ImmutableArray<ICodeGenerator>), GeneratorOptions)  args) 
   { 
      var ((enumInfo, generators), options) = args; 
 
      if (generators.IsDefaultOrEmpty) 
         return; 
 
      foreach (var generator in generators.Distinct()) 
      { 
         var ns = enumInfo.Namespace is null ? null : $"{enumInfo.Namespace}."; 
         var code = generator.Generate(enumInfo, options); 
 
         if (!String.IsNullOrWhiteSpace(code)) 
            context.AddSource($"{ns}{enumInfo.Name}{generator.FileHintSuffix}.g.cs", code); 
      } 
   } 
				
			

The property CounterEnabled is evaluated in the code generator to determine whether to render the counter or not.

				
					public sealed class DemoCodeGenerator : ICodeGenerator 
{ 
   ...
  
   public string Generate(DemoEnumInfo enumInfo, GeneratorOptions options) 
   { 
      var ns = enumInfo.Namespace; 
      var name = enumInfo.Name; 
 
      var sb = new StringBuilder(@"// <auto-generated /> 
#nullable enable"); 
 
      if (options.CounterEnabled) 
      { 
         sb.Append($@" 
 
// generation counter: {Interlocked.Increment(ref _counter)}"); 
      } 
 
      sb.Append($@" 
 
using System.Collections.Generic; 
using DemoLibrary; 
				
			

Packaging of CompilerVisibleProperty

As previously mentioned, the CompilerVisibleProperty can be provided by a NuGet package, so the developers don’t have to do that manually.

In order to do that, create a new file DemoLibrary.props in the project DemoLibrary (right besides DemoLibrary.csproj) with the following content:

				
					<Project> 
 
   <ItemGroup> 
      <CompilerVisibleProperty Include="DemoSourceGenerator_Counter" /> 
   </ItemGroup> 
 
</Project>  
				
			

The DemoLibrary.props must be put into folder build in order to be included by MSBuild automatically.

				
					<Project Sdk="Microsoft.NET.Sdk">

   ...

   <ItemGroup>
      <ProjectReference Include="..\DemoSourceGenerator\DemoSourceGenerator.csproj"
                        PrivateAssets="contentfiles;build"
                        SetTargetFramework="TargetFramework=netstandard2.0" />
      <None Update="DemoLibrary.props" Pack="true" PackagePath="build" />
   </ItemGroup>

</Project>

				
			

Summary

Use MSBuild properties to pass generall, project-wide parameters and use AnalyzerConfigOptionsProvider to receive them.

Kostenloser
Newsletter

Aktuelle Artikel, Screencasts, Webinare und Interviews unserer Experten für Sie

Verpassen Sie keine Inhalte zu Angular, .NET Core, Blazor, Azure und Kubernetes und melden Sie sich zu unserem kostenlosen monatlichen Dev-Newsletter an.

Newsletter Anmeldung
Diese Artikel könnten Sie interessieren
.NET
pg

Smart Enums in .NET: Integration with Frameworks and Libraries

Learn how to seamlessly integrate Smart Enums with essential .NET frameworks and libraries. This article covers practical solutions for JSON serialization, ASP.NET Core model binding for both Minimal APIs and MVC controllers, and Entity Framework Core persistence using value converters. Discover how Thinktecture.Runtime.Extensions provides dedicated packages to eliminate integration friction and maintain type safety across your application stack.
21.09.2025
.NET
pg

Value Objects in .NET: Enhancing Business Semantics

Value objects are fundamental building blocks in Domain-Driven Design, serving far more than simple data wrappers. This article explores their strategic importance in bridging technical code and business concepts, enforcing domain rules, and fostering clearer communication with domain experts. Learn how to build robust aggregates, cultivate ubiquitous language, and encapsulate domain-specific behavior using Thinktecture.Runtime.Extensions in .NET applications.
16.09.2025
.NET
pg

Pattern Matching with Discriminated Unions in .NET

Traditional C# pattern matching with switch statements and if/else chains is error-prone and doesn't guarantee exhaustive handling of all cases. When you add new types or states, it's easy to miss updating conditional logic, leading to runtime bugs. The library Thinktecture.Runtime.Extensions solves this with built-in Switch and Map methods for discriminated unions that enforce compile-time exhaustiveness checking.
26.08.2025
.NET
pg

Value Objects in .NET: Integration with Frameworks and Libraries

Value Objects in .NET provide a structured way to improve consistency and maintainability in domain modeling. This article examines their integration with popular frameworks and libraries, highlighting best practices for seamless implementation. From working with Entity Framework to leveraging their advantages in ASP.NET, we explore how Value Objects can be effectively incorporated into various architectures. By understanding their role in framework integration, developers can optimize data handling and enhance code clarity without unnecessary complexity.
12.08.2025
.NET
pg

Smart Enums: Adding Domain Logic to Enumerations in .NET

This article builds upon the introduction of Smart Enums by exploring their powerful capability to encapsulate behavior, a significant limitation of traditional C# enums. We delve into how Thinktecture.Runtime.Extensions enables embedding domain-specific logic directly within Smart Enum definitions. This co-location of data and behavior promotes more cohesive, object-oriented, and maintainable code, moving beyond scattered switch statements and extension methods. Discover techniques to make your enumerations truly "smart" by integrating behavior directly where it belongs.
29.07.2025
.NET
pg

Discriminated Unions: Representation of Alternative Types in .NET

Representing values that may take on multiple distinct types or states is a common challenge in C#. Traditional approaches—like tuples, generics, or exceptions—often lead to clumsy and error-prone code. Discriminated unions address these issues by enabling clear, type-safe modeling of “one-of” alternatives. This article examines pitfalls of conventional patterns and introduces discriminated unions with the Thinktecture.Runtime.Extensions library, demonstrating how they enhance code safety, prevent invalid states, and improve maintainability—unlocking powerful domain modeling in .NET with minimal boilerplate.
15.07.2025