NetEscapades.EnumGenerators 1.0.0-beta02

This is a prerelease version of NetEscapades.EnumGenerators.
There is a newer prerelease version of this package available.
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                
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" Version="1.0.0-beta02" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add NetEscapades.EnumGenerators --version 1.0.0-beta02                
#r "nuget: NetEscapades.EnumGenerators, 1.0.0-beta02"                
#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 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

Build status NuGet

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.

There are no supported framework assets in this package.

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 (5)

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

Repository Stars
DSharpPlus/DSharpPlus
A .NET library for making bots using the Discord API.
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
xin9le/FastEnum
The world fastest enum utilities for C#/.NET
SteveDunn/Intellenum
Intelligent Enums
Version Downloads Last updated
1.0.0-beta11 16,986 10/24/2024
1.0.0-beta09 100,836 5/15/2024
1.0.0-beta08 169,910 6/5/2023
1.0.0-beta07 77,420 3/10/2023
1.0.0-beta06 29,622 12/20/2022
1.0.0-beta05 260 12/19/2022
1.0.0-beta04 142,815 11/30/2021
1.0.0-beta03 1,908 11/26/2021
1.0.0-beta02 190 11/22/2021
1.0.0-beta01 388 11/18/2021

Initial release of the NuGet package


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