NetEscapades.EnumGenerators
1.0.0-beta02
See the version list below for details.
dotnet add package NetEscapades.EnumGenerators --version 1.0.0-beta02
NuGet\Install-Package NetEscapades.EnumGenerators -Version 1.0.0-beta02
<PackageReference Include="NetEscapades.EnumGenerators" Version="1.0.0-beta02" />
paket add NetEscapades.EnumGenerators --version 1.0.0-beta02
#r "nuget: NetEscapades.EnumGenerators, 1.0.0-beta02"
// Install NetEscapades.EnumGenerators as a Cake Addin #addin nuget:?package=NetEscapades.EnumGenerators&version=1.0.0-beta02&prerelease // Install NetEscapades.EnumGenerators as a Cake Tool #tool nuget:?package=NetEscapades.EnumGenerators&version=1.0.0-beta02&prerelease
NetEscapades.EnumGenerators
A Source Generator package that generates extension methods for enums, to allow fast "reflection".
This source generator requires the .NET 6 SDK. You can target earlier frameworks like .NET Core 3.1 etc, but the SDK must be at least 6.0.100
Add the package to your application using
dotnet add package NetEscapades.EnumGenerators
This will bring in the NetEscapades.EnumGenerators package. This contains the source generator, which automatically adds a marker attribute, [EnumExtensions]
, to your project.
To use the generator, add the [EnumExtensions]
attribute to an enum. For example:
[EnumExtensions]
public MyEnum
{
First,
Second,
}
This will generate a class called MyEnumExtensions
(by default), which contains a number of helper methods. For example:
public static partial class MyEnumExtensions
{
public static string ToStringFast(this MyEnum value)
=> value switch
{
MyEnum.First => nameof(MyEnum.First),
MyEnum.Second => nameof(MyEnum.Second),
_ => value.ToString(),
};
public static bool IsDefined(MyEnum value)
=> value switch
{
MyEnum.First => true,
MyEnum.Second => true,
_ => false,
};
public static bool IsDefined(string name)
=> name switch
{
nameof(MyEnum.First) => true,
nameof(MyEnum.Second) => true,
_ => false,
};
public static bool TryParse(
[System.Diagnostics.CodeAnalysis.NotNullWhen(true)] string? name,
bool ignoreCase,
out MyEnum value)
=> ignoreCase ? TryParseIgnoreCase(name, out value) : TryParse(name, out value);
private static bool TryParseIgnoreCase(
[System.Diagnostics.CodeAnalysis.NotNullWhen(true)] string? name,
out MyEnum value)
{
switch (name)
{
case { } s when s.Equals(nameof(MyEnum.First), System.StringComparison.OrdinalIgnoreCase):
value = MyEnum.First;
return true;
case { } s when s.Equals(nameof(MyEnum.Second), System.StringComparison.OrdinalIgnoreCase):
value = MyEnum.Second;
return true;
case { } s when int.TryParse(name, out var val):
value = (MyEnum)val;
return true;
default:
value = default;
return false;
}
}
public static bool TryParse(
[System.Diagnostics.CodeAnalysis.NotNullWhen(true)] string? name,
out MyEnum value)
{
switch (name)
{
case nameof(MyEnum.First):
value = MyEnum.First;
return true;
case nameof(MyEnum.Second):
value = MyEnum.Second;
return true;
case { } s when int.TryParse(name, out var val):
value = (MyEnum)val;
return true;
default:
value = default;
return false;
}
}
public static MyEnum[] GetValues()
{
return new[]
{
MyEnum.First,
MyEnum.Second,
};
}
public static string[] GetNames()
{
return new[]
{
nameof(MyEnum.First),
nameof(MyEnum.Second),
};
}
}
You can override the name of the extension class by setting ExtensionClassName
in the attribute and/or the namespace of the class by setting ExtensionClassNamespace
. By default, the class will be public if the enum is public, otherwise it will be internal.
Learn more about Target Frameworks and .NET Standard.
-
.NETStandard 2.0
- No dependencies.
NuGet packages (5)
Showing the top 5 NuGet packages that depend on NetEscapades.EnumGenerators:
Package | Downloads |
---|---|
DSharpPlus
A C# API for Discord based off DiscordSharp, but rewritten to fit the API standards. |
|
Indrivo.FileStorage.Accessor.Contracts
Package Description |
|
apex.net-Entities
Models for apex.net |
|
FileStorage.Accessor.Contracts
Package Description |
|
Starnight.Shared
Shared type library for Starnight assemblies |
GitHub repositories (3)
Showing the top 3 popular GitHub repositories that depend on NetEscapades.EnumGenerators:
Repository | Stars |
---|---|
Nexus-Mods/NexusMods.App
Home of the development of the Nexus Mods App
|
|
andrewlock/NetEscapades.EnumGenerators
A source generator for generating fast "reflection" methods for enums
|
|
SteveDunn/Intellenum
Intelligent Enums
|
Version | Downloads | Last updated |
---|---|---|
1.0.0-beta11 | 5,692 | 10/24/2024 |
1.0.0-beta09 | 85,332 | 5/15/2024 |
1.0.0-beta08 | 163,538 | 6/5/2023 |
1.0.0-beta07 | 72,616 | 3/10/2023 |
1.0.0-beta06 | 28,739 | 12/20/2022 |
1.0.0-beta05 | 253 | 12/19/2022 |
1.0.0-beta04 | 133,804 | 11/30/2021 |
1.0.0-beta03 | 1,904 | 11/26/2021 |
1.0.0-beta02 | 181 | 11/22/2021 |
1.0.0-beta01 | 371 | 11/18/2021 |
Initial release of the NuGet package
See https://github.com/andrewlock/NetEscapades.EnumGenerators/blob/master/CHANGELOG.md#v100 for more details.