NetEscapades.EnumGenerators.Interceptors 1.0.0-beta12

This is a prerelease version of NetEscapades.EnumGenerators.Interceptors.
dotnet add package NetEscapades.EnumGenerators.Interceptors --version 1.0.0-beta12                
NuGet\Install-Package NetEscapades.EnumGenerators.Interceptors -Version 1.0.0-beta12                
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="NetEscapades.EnumGenerators.Interceptors" Version="1.0.0-beta12" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add NetEscapades.EnumGenerators.Interceptors --version 1.0.0-beta12                
#r "nuget: NetEscapades.EnumGenerators.Interceptors, 1.0.0-beta12"                
#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.
// Install NetEscapades.EnumGenerators.Interceptors as a Cake Addin
#addin nuget:?package=NetEscapades.EnumGenerators.Interceptors&version=1.0.0-beta12&prerelease

// Install NetEscapades.EnumGenerators.Interceptors as a Cake Tool
#tool nuget:?package=NetEscapades.EnumGenerators.Interceptors&version=1.0.0-beta12&prerelease                

alternate text is missing from this package README image NetEscapades.EnumGenerators.Interceptors

A source generator interceptor for automatically intercepting calls to ToString() on enums, and replacing them with calls to ToStringFast() generated by NetEscapades.EnumGenerators

This source generator requires the .NET 8.0.400 SDK. You can target earlier frameworks like .NET Core 3.1 etc, but the SDK must be at least 8.0.400

Why use this package?

Many methods that operate with enums, such as the ToString() or HasFlag() method, are surprisingly slow. The NetEscapades.EnumGenerators uses a source generator to provide fast versions of these methods, such as ToStringFast() or HasFlagFast().

The main downside to the extension methods generated by NetEscapades.EnumGenerators is that you have to remember to use them. The NetEscapades.EnumGenerators.Interceptors package solves this problem by intercepting calls to ToString() and replacing them with calls ToStringFast() automatically using the .NET compiler feature called interceptors.

Interceptors were introduced as an experimental feature in C#12 with .NET 8, and are stable in .NET 9. They allow a source generator to "intercept" certain method calls, and replace the call with a different one.

For example if you have this code:

var choice = Color.Red;
Console.WriteLine("You chose: " + choice.ToString());

public enum Color
{
    Red = 0,
    Blue = 1,
}

When you use the NetEscapades.EnumGenerators.Interceptors, the interceptor automatically replaces the call to choice.ToString() at compile-time with a call to ToStringFast(), as though you wrote the following:

// The compiler replaces the call with this 👇 
Console.WriteLine("You chose: " + choice.ToStringFast());

There are many caveats to this behaviour, as described below, but any explicit calls to ToString() or HasFlag() on a supported enum are replaced automatically.

Adding NetEscapades.EnumGenerators.Interceptors to your project

Add the package to your application using:

dotnet add package NetEscapades.EnumGenerators.Interceptors

This adds a <PackageReference> to your project. You can additionally mark the package as PrivateAssets="all" and ExcludeAssets="runtime".

Setting PrivateAssets="all" means any projects referencing this one won't get a reference to the NetEscapades.EnumGenerators.Interceptors package. Setting ExcludeAssets="runtime" ensures the NetEscapades.EnumGenerators.Interceptors.Attributes.dll file is not copied to your build output (it is not required at runtime).

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

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net8.0</TargetFramework>
  </PropertyGroup>

  
  <PackageReference Include="NetEscapades.EnumGenerators.Interceptors" Version="1.0.0-beta12" 
    PrivateAssets="all" ExcludeAssets="runtime" />
  

</Project>

Enabling interception for an enum

By default, adding NetEscapades.EnumGenerators.Interceptors to a project enables interception for all enums defined in that project that use the [EnumExtensions] or [EnumExtensions<T>] attributes. If you wish to intercept calls made to enums with extensions defined in other projects, you must add the [Interceptable<T>] attribute in the project where you want the interception to happen, e.g.

[assembly:Interceptable<DateTimeKind>]
[assembly:Interceptable<Color>]

If you don't want a specific enum to be intercepted, you can set the IsInterceptable property to false, e.g.

[EnumExtensions(IsInterceptable = false)]
public enum Colour
{
    Red = 0,
    Blue = 1,
}

Interception only works when the target type is unambiguously an interceptable enum, so it won't work

  • When ToString() is called in other source generated code.
  • When ToString() is called in already-compiled code.
  • If the ToString() call is implicit (for example in string interpolation)
  • If the ToString() call is made on a base type, such as System.Enum or object
  • If the ToString() call is made on a generic type

For example:

// All the examples in this method CAN be intercepted
public void CanIntercept()
{
    var ok1 = Color.Red.ToString(); // ✅
    var red = Color.Red;
    var ok2 = red.ToString(); // ✅
    var ok3 = "The colour is " + red.ToString(); // ✅
    var ok4 = $"The colour is {red.ToString()}"; // ✅
}

// The examples in this method can NOT be intercepted
public void CantIntercept()
{
    var bad1 = ((System.Enum)Color.Red).ToString(); // ❌ Base type
    var bad2 = ((object)Color.Red).ToString(); // ❌ Base type
    
    var bad3 = "The colour is " + red; // ❌ implicit
    var bad4 = $"The colour is {red}"; // ❌ implicit

    string Write<T>(T val)
        where T : Enum
    {
        return val.ToString(); // ❌ generic
    }
}
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. 
.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 net451 is compatible.  net452 was computed.  net46 was computed.  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.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories (1)

Showing the top 1 popular GitHub repositories that depend on NetEscapades.EnumGenerators.Interceptors:

Repository Stars
andrewlock/NetEscapades.EnumGenerators
A source generator for generating fast "reflection" methods for enums
Version Downloads Last updated
1.0.0-beta12 74 1/26/2025

Breaking Changes:
- By default, `ToStringFast()` no longer uses `[DisplayName]` and `[Description]` by default. The original behaviour can be restored by passing `allowMatchingMetadataAttribute:true` (#122)
- Split the experimental interceptor support into a separate project, NetEscapades.EnumGenerators.Interceptors (#125)
- Enable interception by default when NetEscapades.EnumGenerators.Interceptors is added (#127)

Features
- Added a package logo (#125)

Fixes
- Fixed indentation in generated code so it aligns properly with 4 spaces (#120) Thanks @karl-sjogren!
- Fix missing global on System namespace usages (#118) Thanks @henrikwidlund!
- Don't use `using`s in generated code (#129)


See https://github.com/andrewlock/NetEscapades.EnumGenerators/blob/main/CHANGELOG.md#v100-beta12 for more details.