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
<PackageReference Include="MBW.Generators.GeneratorHelpers" Version="0.3.0" />
<PackageVersion Include="MBW.Generators.GeneratorHelpers" Version="0.3.0" />
<PackageReference Include="MBW.Generators.GeneratorHelpers" />
paket add MBW.Generators.GeneratorHelpers --version 0.3.0
#r "nuget: MBW.Generators.GeneratorHelpers, 0.3.0"
#:package MBW.Generators.GeneratorHelpers@0.3.0
#addin nuget:?package=MBW.Generators.GeneratorHelpers&version=0.3.0
#tool nuget:?package=MBW.Generators.GeneratorHelpers&version=0.3.0
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.GeneratorHelperspackage. - Apply
[GenerateSymbolExtensions]to aclassorstructcontaining annotatedconst stringfields. - Override the generated extensions class name and namespace via the attribute’s
NameandNamespaceproperties. Defaults to<TypeName>ExtensionsinMicrosoft.CodeAnalysis. - Per-field
MethodNameoverrides customise the method suffix for[SymbolNameExtension]and[NamespaceNameExtension]. - Fully qualified strings support the
global::prefix, nested types using+, and generic metadata names likeDictionary`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
ISymbolandINamespaceSymboloverloads 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 stringfield containing a fully qualified type name.- Generates:
IsNamedExactlyTypeN(this ISymbol)- tests if the symbol is exactly named N
- Generates:
- [NamespaceNameExtension] – place on a
const stringfield containing a fully qualified namespace.- Generates:
IsInNamespaceN(this ISymbol)- tests if the symbol is in the namespace N, or any namespace under NIsExactlyInNamespaceN(this ISymbol)- tests if the symbol is exactly in the namespace NIsInNamespaceN(this INamespaceSymbol)- tests if the namespace symbol is in the namespace N, or any namespace under NIsExactlyNamespaceN(this INamespaceSymbol)- tests if the namespace symbol is exactly the namespace N
- Generates:
More information
This project is provided as-is without support. Additional examples will be available in the tests.
| Product | Versions 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. |
-
.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 |