FastEnumToString 2.0.0

Suggested Alternatives

FastEnum.Extensions.Generator

The owner has unlisted this package. This could mean that the package is deprecated, has security vulnerabilities or shouldn't be used anymore.
dotnet add package FastEnumToString --version 2.0.0
                    
NuGet\Install-Package FastEnumToString -Version 2.0.0
                    
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="FastEnumToString" Version="2.0.0">
  <PrivateAssets>all</PrivateAssets>
  <IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
</PackageReference>
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="FastEnumToString" Version="2.0.0" />
                    
Directory.Packages.props
<PackageReference Include="FastEnumToString">
  <PrivateAssets>all</PrivateAssets>
  <IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
</PackageReference>
                    
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 FastEnumToString --version 2.0.0
                    
#r "nuget: FastEnumToString, 2.0.0"
                    
#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.
#:package FastEnumToString@2.0.0
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=FastEnumToString&version=2.0.0
                    
Install as a Cake Addin
#tool nuget:?package=FastEnumToString&version=2.0.0
                    
Install as a Cake Tool

FastEnumToString

Source Generator generating ToString extension methods for any enums.

Configure

Global configuration: To set the fallback behaviour if no matching value is found.

<PropertyGroup>
    <FastEnumDefaultBehaviour>Default</FastEnumDefaultBehaviour>
</PropertyGroup>

Or you can use the [ToString] attribute to override the global configuration by calling its parameterized constructor:

[ToString(ToStringDefault.First)]

Available values (for both):

  • Default: Uses the built-in Enum class behaviour and converts the provided value to its numeric representation
  • First: uses the first avalilable enum member,
  • Throw: throwing an ArgumentOutOfRangeException

*the csproj configuration is case insensitive

Example

using FastEnumToString;

using MyNamespace;

Color color = (Color)5;

Console.WriteLine(color);
Console.WriteLine(color.FastToString());
Console.WriteLine(NestingClass<int, List<int>>.NestedInClassEnum.None.FastToString());
Console.WriteLine(EnumStringConverter.FastToString(NestingClass<int, List<int>>.NestedInClassEnum.None));

namespace MyNamespace
{
    public class NestingClass<T, K>
        where T : struct
        where K : class, new()
    {
        [ToString]
        public enum NestedInClassEnum
        {
            None
        }
    }
    
    [ToString(ToStringDefault.First)]
    public enum Color
    {
        Red,
        Green,
        Blue,
    }

    // Flags are not fully supported,
    // only distinct values can be stringified
    [ToString, Flags]
    public enum Devices
    {
        Phone = 1,
        TV = 2,
        Watch = 4,
    }
}

The followning will be generated:

// <auto-generated/>

namespace FastEnumToString
{
    [global::System.CodeDom.Compiler.GeneratedCode("FastEnumToString", "2.0.0")]
    public static class EnumStringConverter
    {
        public static string FastToString<T, K>(this MyNamespace.NestingClass<T, K>.NestedInClassEnum enumValue)
            where T : struct
            where K : class, new()
        => enumValue switch
        {
            MyNamespace.NestingClass<T, K>.NestedInClassEnum.None => nameof(MyNamespace.NestingClass<T, K>.NestedInClassEnum.None),
            _ => ((int)enumValue).ToString()
        };

        public static string FastToString(this MyNamespace.Color enumValue) => enumValue switch
        {
            MyNamespace.Color.Red => nameof(MyNamespace.Color.Red),
            MyNamespace.Color.Green => nameof(MyNamespace.Color.Green),
            MyNamespace.Color.Blue => nameof(MyNamespace.Color.Blue),
            _ => nameof(MyNamespace.Color.Red)
        };

        public static string FastToString(this MyNamespace.Devices enumValue) => enumValue switch
        {
            MyNamespace.Devices.Phone => nameof(MyNamespace.Devices.Phone),
            MyNamespace.Devices.TV => nameof(MyNamespace.Devices.TV),
            MyNamespace.Devices.Watch => nameof(MyNamespace.Devices.Watch),
            _ => ((int)enumValue).ToString()
        };
    }
}
There are no supported framework assets in this package.

Learn more about Target Frameworks and .NET Standard.

  • .NETStandard 2.0

    • No dependencies.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated

Default behaviour changed!