NetEscapades.EnumGenerators.Interceptors
1.0.0-beta18
See the version list below for details.
dotnet add package NetEscapades.EnumGenerators.Interceptors --version 1.0.0-beta18
NuGet\Install-Package NetEscapades.EnumGenerators.Interceptors -Version 1.0.0-beta18
<PackageReference Include="NetEscapades.EnumGenerators.Interceptors" Version="1.0.0-beta18" />
<PackageVersion Include="NetEscapades.EnumGenerators.Interceptors" Version="1.0.0-beta18" />
<PackageReference Include="NetEscapades.EnumGenerators.Interceptors" />
paket add NetEscapades.EnumGenerators.Interceptors --version 1.0.0-beta18
#r "nuget: NetEscapades.EnumGenerators.Interceptors, 1.0.0-beta18"
#:package NetEscapades.EnumGenerators.Interceptors@1.0.0-beta18
#addin nuget:?package=NetEscapades.EnumGenerators.Interceptors&version=1.0.0-beta18&prerelease
#tool nuget:?package=NetEscapades.EnumGenerators.Interceptors&version=1.0.0-beta18&prerelease
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.
In addition to the caveats listed below, the interceptor approach unfortunately requires boxing the enum, so in general we recommend enabling the usage analyzers in NetEscapades.EnumGenerators instead of using this package.
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".
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
</PropertyGroup>
<PackageReference Include="NetEscapades.EnumGenerators.Interceptors" Version="1.0.0-beta18" />
</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 instringinterpolation) - If the
ToString()call is made on a base type, such asSystem.Enumorobject - 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 | 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 | 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. |
-
.NETStandard 2.0
- NetEscapades.EnumGenerators (>= 1.0.0-beta18)
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 |
|---|---|---|
| 1.0.0-beta21 | 1,843 | 3/9/2026 |
| 1.0.0-beta20 | 1,599 | 1/15/2026 |
| 1.0.0-beta19 | 379 | 1/5/2026 |
| 1.0.0-beta18 | 81 | 1/5/2026 |
| 1.0.0-beta16 | 815 | 11/4/2025 |
| 1.0.0-beta15 | 201 | 10/29/2025 |
| 1.0.0-beta14 | 773 | 6/15/2025 |
| 1.0.0-beta13 | 661 | 5/12/2025 |
| 1.0.0-beta12 | 371 | 1/26/2025 |
### Breaking Changes
* Add support for generating `ReadOnlySpan<char>` based methods when System.Memory is present (#182, #212)
### Features
* Add support for disabling number parsing using `EnumParseOptions` (#175)
* Add support for automatic `ToLowerInvariant` and `ToUpperInvariant` in `ToStringFast()`, using `SerializationOptions` (#177)
* Add Analyzers to encourage using the generated methods
* Add analyzer to detect ToString() on enums with [EnumExtensions] or EnumExtensions<T> (#196)
* Extend ToStringAnalyzer to detect enum usage in string interpolation (#198)
* Add analyzer to detect HasFlag() and suggest HasFlagFast() replacement (#199)
* Extract the extension class and namespace in analyzers (#200)
* Add using directive support to HasFlagAnalyzer and ToStringAnalyzer code fixes (#202)
* Add analyzer for Enum.IsDefined() suggesting generated IsDefined method (#203)
* Add analyzer for Enum.Parse() suggesting generated Parse method (NEEG007) (#204)
* Add analyzer for Enum.GetNames() with generated alternative (#209)
* Add analyzer for Enum.GetValues() with code fix to use generated method (#207)
* Add analyzer for Enum.GetValuesAsUnderlyingType() usage (#208)
* Add analyzer for Enum.TryParse() usage (#206)
* Refactor analyzers to reduce some duplication (#205)
* Disable usage analyzers by default, enable via editorconfig (#214)
* Update default severity to warning (#218)
* Change how the usage analyzers are enabled to use an `EnumGenerator_EnableUsageAnalyzers` MSBuild property instead (#222)
* Add AnalyzerTests project to confirm that the analyzers are actually triggering warnings in a real project (#223)
* Mention globalconfig in the docs, and tweak the detection for consistency (#224)
### Fixes
* Remove ToStringFastWithMetadata when it's not needed for perf reasons(#176)
* Minor performance fixes in generated code (#178)
* Use Collection expressions in generated code if possible (#179)
### Misc
* Build tweaks (#180)
* Add support for .NET 10.0 in Benchmark project (#184) Thanks [@HakamFostok](https://github.com/HakamFostok)!
* Add Usage Analyzers documentation to README (#216)
* Update Readme (#219)
See https://github.com/andrewlock/NetEscapades.EnumGenerators/blob/main/CHANGELOG.md#v100-beta18 for more details.