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.
<PackageVersion Include="NetEscapades.EnumGenerators" Version="1.0.0-beta02" />
                    
Directory.Packages.props
<PackageReference Include="NetEscapades.EnumGenerators" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version 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.
#addin nuget:?package=NetEscapades.EnumGenerators&version=1.0.0-beta02&prerelease
                    
Install NetEscapades.EnumGenerators as a Cake Addin
#tool nuget:?package=NetEscapades.EnumGenerators&version=1.0.0-beta02&prerelease
                    
Install NetEscapades.EnumGenerators as a Cake Tool

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

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

BVP.Common.NET

Most important reusable code for BVP

apex.net-Entities

Models for apex.net

FileStorage.Accessor.Contracts

Package Description

GitHub repositories (6)

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

Repository Stars
Nexus-Mods/NexusMods.App
Home of the development of the Nexus Mods App
DSharpPlus/DSharpPlus
A .NET library for making bots using the Discord API.
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
Kengxxiao/CelestiteLauncher
Cross platform third-party DMM Game Player in C#
Version Downloads Last updated
1.0.0-beta12 32,897 1/26/2025
1.0.0-beta11 53,272 10/24/2024
1.0.0-beta09 152,678 5/15/2024
1.0.0-beta08 189,444 6/5/2023
1.0.0-beta07 100,619 3/10/2023
1.0.0-beta06 32,298 12/20/2022
1.0.0-beta05 278 12/19/2022
1.0.0-beta04 182,497 11/30/2021
1.0.0-beta03 1,927 11/26/2021
1.0.0-beta02 213 11/22/2021
1.0.0-beta01 444 11/18/2021

Initial release of the NuGet package


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