MBW.Generators.GeneratorHelpers 0.3.0

dotnet add package MBW.Generators.GeneratorHelpers --version 0.3.0
                    
NuGet\Install-Package MBW.Generators.GeneratorHelpers -Version 0.3.0
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="MBW.Generators.GeneratorHelpers" Version="0.3.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="MBW.Generators.GeneratorHelpers" Version="0.3.0" />
                    
Directory.Packages.props
<PackageReference Include="MBW.Generators.GeneratorHelpers" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add MBW.Generators.GeneratorHelpers --version 0.3.0
                    
#r "nuget: MBW.Generators.GeneratorHelpers, 0.3.0"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package MBW.Generators.GeneratorHelpers@0.3.0
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=MBW.Generators.GeneratorHelpers&version=0.3.0
                    
Install as a Cake Addin
#tool nuget:?package=MBW.Generators.GeneratorHelpers&version=0.3.0
                    
Install as a Cake Tool

MBW.Generators.GeneratorHelpers

About

Provides allocation-free extension methods for Roslyn symbols based on predefined type and namespace strings. Annotate a type with constants representing canonical symbols and the generator emits helper methods to quickly compare symbols without allocations or ToDisplayString calls.

Useful when analyzing large code bases where repeated string construction would otherwise hurt performance.

Quick Start

  • Install the MBW.Generators.GeneratorHelpers package.
  • Apply [GenerateSymbolExtensions] to a class or struct containing annotated const string fields.
  • Override the generated extensions class name and namespace via the attribute’s Name and Namespace properties. Defaults to <TypeName>Extensions in Microsoft.CodeAnalysis.
  • Per-field MethodName overrides customise the method suffix for [SymbolNameExtension] and [NamespaceNameExtension].
  • Fully qualified strings support the global:: prefix, nested types using +, and generic metadata names like Dictionary`2.
  • The package includes both the attributes and the source generator.

Example

using MBW.Generators.GeneratorHelpers;
using MBW.Generators.GeneratorHelpers.Attributes;

// Control the generated type, Name and Namespace are optional - the default is 'Microsoft.CodeAnalysis'
[GenerateSymbolExtensions(Name = "KnownExtensions", Namespace = "My.Helpers")]
internal static class Known
{
    // Generate 
    [SymbolNameExtension(MethodName = "Exception")]
    public const string ExceptionType = "global::System.Exception";
}

Generates

// <auto-generated/>
using System;
using Microsoft.CodeAnalysis;

namespace My.Helpers;

internal static class KnownExtensions
{
    public static bool IsNamedExactlyTypeException(this ISymbol? symbol)
    {
        if (symbol is null) return false;
        if (!symbol.Name.Equals("Exception", StringComparison.Ordinal)) return false;
        if (symbol is not INamedTypeSymbol t0) return false;
        if (t0.ContainingType is not null) return false;
        var ns = t0.ContainingNamespace;
        if (ns is null || !ns.Name.Equals("System", StringComparison.Ordinal)) return false;
        ns = ns.ContainingNamespace;
        return ns != null && ns.IsGlobalNamespace;
    }
}

Features

  • Generates allocation-free helpers for types and namespaces.
  • Handles nested and generic types, including names with global:: and multiple containing types.
  • Provides ISymbol and INamespaceSymbol overloads for namespace checks.
  • Supports custom extension class name/namespace and per-field method name overrides.
  • Benchmarks show this approach is 10 times faster than performing a ToDisplayString() and then string equality

Attributes

  • [GenerateSymbolExtensions] – apply to a type to opt in. Controls generated class name and namespace.
  • [SymbolNameExtension] – place on a const string field containing a fully qualified type name.
    • Generates:
      • IsNamedExactlyTypeN(this ISymbol) - tests if the symbol is exactly named N
  • [NamespaceNameExtension] – place on a const string field containing a fully qualified namespace.
    • Generates:
      • IsInNamespaceN(this ISymbol) - tests if the symbol is in the namespace N, or any namespace under N
      • IsExactlyInNamespaceN(this ISymbol) - tests if the symbol is exactly in the namespace N
      • IsInNamespaceN(this INamespaceSymbol) - tests if the namespace symbol is in the namespace N, or any namespace under N
      • IsExactlyNamespaceN(this INamespaceSymbol) - tests if the namespace symbol is exactly the namespace N

More information

This project is provided as-is without support. Additional examples will be available in the tests.

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • .NETStandard 2.0

    • No dependencies.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
0.3.0 203 9/1/2025