![]() |
VOOZH | about |
dotnet add package NetEscapades.EnumGenerators.Interceptors --version 1.0.0-beta21
NuGet\Install-Package NetEscapades.EnumGenerators.Interceptors -Version 1.0.0-beta21
<PackageReference Include="NetEscapades.EnumGenerators.Interceptors" Version="1.0.0-beta21" />
<PackageVersion Include="NetEscapades.EnumGenerators.Interceptors" Version="1.0.0-beta21" />Directory.Packages.props
<PackageReference Include="NetEscapades.EnumGenerators.Interceptors" />Project file
paket add NetEscapades.EnumGenerators.Interceptors --version 1.0.0-beta21
#r "nuget: NetEscapades.EnumGenerators.Interceptors, 1.0.0-beta21"
#:package NetEscapades.EnumGenerators.Interceptors@1.0.0-beta21
#addin nuget:?package=NetEscapades.EnumGenerators.Interceptors&version=1.0.0-beta21&prereleaseInstall as a Cake Addin
#tool nuget:?package=NetEscapades.EnumGenerators.Interceptors&version=1.0.0-beta21&prereleaseInstall as a Cake Tool
An experimental 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
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.
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-beta21" />
</Project>
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
ToString() is called in other source generated code.ToString() is called in already-compiled code.ToString() call is implicit (for example in string interpolation)ToString() call is made on a base type, such as System.Enum or objectToString() call is made on a generic typeFor 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 net5.0 was computed. net5.0-windows net5.0-windows was computed. net6.0 net6.0 was computed. net6.0-android net6.0-android was computed. net6.0-ios net6.0-ios was computed. net6.0-maccatalyst net6.0-maccatalyst was computed. net6.0-macos net6.0-macos was computed. net6.0-tvos net6.0-tvos was computed. net6.0-windows net6.0-windows was computed. net7.0 net7.0 was computed. net7.0-android net7.0-android was computed. net7.0-ios net7.0-ios was computed. net7.0-maccatalyst net7.0-maccatalyst was computed. net7.0-macos net7.0-macos was computed. net7.0-tvos net7.0-tvos was computed. net7.0-windows net7.0-windows was computed. net8.0 net8.0 was computed. net8.0-android net8.0-android was computed. net8.0-browser net8.0-browser was computed. net8.0-ios net8.0-ios was computed. net8.0-maccatalyst net8.0-maccatalyst was computed. net8.0-macos net8.0-macos was computed. net8.0-tvos net8.0-tvos was computed. net8.0-windows net8.0-windows was computed. net9.0 net9.0 was computed. net9.0-android net9.0-android was computed. net9.0-browser net9.0-browser was computed. net9.0-ios net9.0-ios was computed. net9.0-maccatalyst net9.0-maccatalyst was computed. net9.0-macos net9.0-macos was computed. net9.0-tvos net9.0-tvos was computed. net9.0-windows net9.0-windows was computed. net10.0 net10.0 was computed. net10.0-android net10.0-android was computed. net10.0-browser net10.0-browser was computed. net10.0-ios net10.0-ios was computed. net10.0-maccatalyst net10.0-maccatalyst was computed. net10.0-macos net10.0-macos was computed. net10.0-tvos net10.0-tvos was computed. net10.0-windows net10.0-windows was computed. |
| .NET Core | netcoreapp2.0 netcoreapp2.0 was computed. netcoreapp2.1 netcoreapp2.1 was computed. netcoreapp2.2 netcoreapp2.2 was computed. netcoreapp3.0 netcoreapp3.0 was computed. netcoreapp3.1 netcoreapp3.1 was computed. |
| .NET Standard | netstandard2.0 netstandard2.0 is compatible. netstandard2.1 netstandard2.1 was computed. |
| .NET Framework | net451 net451 is compatible. net452 net452 was computed. net46 net46 was computed. net461 net461 was computed. net462 net462 was computed. net463 net463 was computed. net47 net47 was computed. net471 net471 was computed. net472 net472 was computed. net48 net48 was computed. net481 net481 was computed. |
| MonoAndroid | monoandroid monoandroid was computed. |
| MonoMac | monomac monomac was computed. |
| MonoTouch | monotouch monotouch was computed. |
| Tizen | tizen40 tizen40 was computed. tizen60 tizen60 was computed. |
| Xamarin.iOS | xamarinios xamarinios was computed. |
| Xamarin.Mac | xamarinmac xamarinmac was computed. |
| Xamarin.TVOS | xamarintvos xamarintvos was computed. |
| Xamarin.WatchOS | xamarinwatchos xamarinwatchos was computed. |
This package is not used by any NuGet packages.
This package is not used by any popular GitHub repositories.
| Version | Downloads | Last Updated |
|---|---|---|
| 1.0.0-beta21 | 2,162 | 3/9/2026 |
| 1.0.0-beta20 | 1,623 | 1/15/2026 |
| 1.0.0-beta19 | 380 | 1/5/2026 |
| 1.0.0-beta18 | 83 | 1/5/2026 |
| 1.0.0-beta16 | 838 | 11/4/2025 |
| 1.0.0-beta15 | 203 | 10/29/2025 |
| 1.0.0-beta14 | 777 | 6/15/2025 |
| 1.0.0-beta13 | 686 | 5/12/2025 |
| 1.0.0-beta12 | 374 | 1/26/2025 |
## 1.0.0-beta21
### Breaking Changes
* When using the _NetEscapades.EnumGenerators_ meta package, set `EnumGenerator_PreserveAttributes=true` by default, so that usage analyzers can light up in downstream projects (#241)
### Features
* Add "Force Internal" to allow forcing extension methods to be `internal` even for `public` enums (#230) Thanks [@xC0dex](https://github.com/xC0dex)!
* Extend usage analyzers to nullable enum variables (#244)
### Fixes
* Scan all assemblies for "external" attributes for usage analyzers (#238)
See https://github.com/andrewlock/NetEscapades.EnumGenerators/blob/main/CHANGELOG.md#v100-beta21 for more details.