NetEscapades.EnumGenerators 1.0.0-beta03

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-beta03                
NuGet\Install-Package NetEscapades.EnumGenerators -Version 1.0.0-beta03                
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-beta03" />                
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-beta03                
#r "nuget: NetEscapades.EnumGenerators, 1.0.0-beta03"                
#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-beta03&prerelease

// Install NetEscapades.EnumGenerators as a Cake Tool
#tool nuget:?package=NetEscapades.EnumGenerators&version=1.0.0-beta03&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.

CS0436 and [InternalsVisibleTo]

The source generator automatically adds the [EnumExtensions] attributes to your compilation as an internal attribute. If you add the source generator package to multiple projects, and use the [InternalsVisibleTo] attribute, you may experience errors when you build:

warning CS0436: The type 'EnumExtensionsAttribute' in 'NetEscapades.EnumGenerators\EnumExtensionsAttribute.cs' conflicts with the imported type 'EnumExtensionsAttribute' in 'MyProject, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'.

Removing the [InternalsVisibleTo] attribute will resolve the problem, but if this is not possible you can disable the auto-generation of the [EnumExtensions] marker attributes, and rely on the helper NetEscapades.EnumExtensions.Attributes package instead. This package contains the same attributes, but as they are in an external package, you can avoid the CS0436 error.

Add the package to your solution, ensuring you set "PrivateAssets="All" in the <PackageReference>. To disable the auto-generation of the marker attributes, define the constant NETESCAPADES_ENUMGENERATORS_EXCLUDE_ATTRIBUTES in your project file. Your project file should look something like the following:

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net6.0</TargetFramework>
    <DefineConstants>NETESCAPADES_ENUMGENERATORS_EXCLUDE_ATTRIBUTES</DefineConstants>
  </PropertyGroup>
  
  
  <PackageReference Include="NetEscapades.EnumExtensions" Version="1.0.0-beta03" />
  <PackageReference Include="NetEscapades.EnumExtensions.Attributes" Version="1.0.0-beta03">
    <PrivateAssets>All</PrivateAssets>
  </PackageReference>
  

</Project>

The attribute library is only required at compile time, so it won't appear in your build output.

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