EnumSerializer 2.3.0

dotnet add package EnumSerializer --version 2.3.0
                    
NuGet\Install-Package EnumSerializer -Version 2.3.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="EnumSerializer" Version="2.3.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="EnumSerializer" Version="2.3.0" />
                    
Directory.Packages.props
<PackageReference Include="EnumSerializer">
  <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 EnumSerializer --version 2.3.0
                    
#r "nuget: EnumSerializer, 2.3.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 EnumSerializer@2.3.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=EnumSerializer&version=2.3.0
                    
Install as a Cake Addin
#tool nuget:?package=EnumSerializer&version=2.3.0
                    
Install as a Cake Tool

EnumSerializer

Test Version Download MIT License

Zero-allocation serialization and deserialization of enums to and from strings enums in C#.

Installation

You can install the EnumSerializer from NuGet.

Usage

Extension methods for enum serialization are generated in the same namespace as the enum. Simply mark your enum with the [EnumSerializable] attribute with the appropriate attributes to generate the serialization code.

using EnumSerializer;

namespace Test;

[EnumSerializable(typeof(DefaultSerializeValueAttribute))]
internal enum MyEnum
{
    [DefaultSerializeValue("val1")]
    Value1,

    [DefaultSerializeValue("val2")]
    Value2,

    [DefaultSerializeValue("val3")]
    Value3
}

This will generate the following extension methods for the MyEnum enum:

namespace Test
{
    internal static class MyEnumSerializationExtensions
    {
        internal static string ToString<TAttr>(this MyEnum value) where TAttr : SerializeValueAttribute
        {
            if (typeof(TAttr) == typeof(DefaultSerializeValueAttribute))
                return ToDefaultSerializeValue(value);

            // Fallback to default ToString() if no matching attribute type is found
            return value.ToString();
        }

        internal static string ToDefaultSerializeValue(this MyEnum value)
        {
            return value switch
            {
                MyEnum.Value1 => "val1",
                MyEnum.Value2 => "val2",
                MyEnum.Value3 => "val3",
                _ => value.ToString(),
            };
        }

        internal static bool TryParseMyEnum<TAttr>(this ReadOnlySpan<char> text, out MyEnum value) where TAttr : SerializeValueAttribute
        {
            if (typeof(TAttr) == typeof(DefaultSerializeValueAttribute))
                return TryParseMyEnumFromDefaultSerializeValue(text, out value);

            // No matching attribute type found
            value = default;
            return false;
        }

        internal static bool TryParseMyEnumFromDefaultSerializeValue(this ReadOnlySpan<char> text, out MyEnum value)
        {
            switch (text)
            {
                case "val1":
                    value = MyEnum.Value1;
                    return true;
                case "val2":
                    value = MyEnum.Value2;
                    return true;
                case "val3":
                    value = MyEnum.Value3;
                    return true;
            }

            value = default;
            return false;
        }
    }
}

Note that this example is simplified for demonstration purposes. The actual generated code may include additional features such as optimizations.

Multiple attribute types can be usesd for the same enum to generate different serialization methods.

using EnumSerializer;

namespace Test;

[EnumSerializable(typeof(DefaultSerializeValueAttribute))]
[EnumSerializable(typeof(UIValueAttribute))]
internal enum MyEnum
{
    [DefaultSerializeValue("val1")]
    [UIValue("Value 1")]
    Value1,

    [DefaultSerializeValue("val2")]
    [UIValue("Value 2")]
    Value2,

    [DefaultSerializeValue("val3")]
    [UIValue("Value 3")]
    Value3
}

Options

Case sensitivity for parsing

To enable case-insensitive parsing, set CaseSensitive property to false in the EnumSerializableAttribute.

using EnumSerializer;

namespace Test;

[EnumSerializable(typeof(DefaultSerializeValueAttribute), CaseSensitive = false)]
internal enum MyEnum
{
    [DefaultSerializeValue("val1")]
    Value1,

    [DefaultSerializeValue("val2")]
    Value2,

    [DefaultSerializeValue("val3")]
    Value3
}

This will generate parsing methods that ignore case when matching input strings to enum values. The default value for CaseSensitive is true, meaning that parsing will be case-sensitive unless explicitly set to false.

Target methods

By default, the generator creates ToString() and TryParse() methods for each enum. To customize the generated method names, you can set the Methods property in the EnumSerializableAttribute. The Methods property accepts a combination of flags from the ExtensionMethods enum, which includes the following options:

  • ToString: Generates a ToString() extension method for the enum.
  • TryParse: Generates a TryParse() extension method for the enum.
  • All: Generates both ToString() and TryParse() extension methods for the enum.

(ExtensionMethods enum also has a None value which can be used to disable generation of all methods, but this is not a common use case since it would defeat the purpose of using the generator.)

For example, following code will generate only ToString() methods for the MyEnum enum:

using EnumSerializer;

namespace Test;

[EnumSerializable(typeof(DefaultSerializeValueAttribute), Methods = ExtensionMethods.ToString)]
internal enum MyEnum
{
    [DefaultSerializeValue("val1")]
    Value1,

    [DefaultSerializeValue("val2")]
    Value2,

    [DefaultSerializeValue("val3")]
    Value3
}

Benchmarks

Parse performance

Parse 10 enum values from their string representations using both the standard Enum<T>.TryParse() method and the generated try-parse method from this generator.

Method Mean Error StdDev Ratio RatioSD
ParseStandard 244.33 ns 2.347 ns 2.195 ns 1.00 0.00
ParseGenerated 31.19 ns 0.446 ns 0.395 ns 0.13 0.00
ParseStandardIgnoreCase 337.83 ns 3.093 ns 2.894 ns 1.38 0.02
ParseGeneratedIgnoreCase 121.84 ns 0.544 ns 0.425 ns 0.50 0.00

Warning for security-sensitive data

Do NOT use this generator in security-sensitive contexts, because the generated code is optimized for performance and may be vulnerable to certain types of attacks (e.g., timing attacks). For such scenarios, consider using a more secure serialization approach that prioritizes security over performance.

There are no supported framework assets in this package.

Learn more about Target Frameworks and .NET Standard.

This package has 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
2.3.0 47 4/2/2026
2.2.0 43 4/1/2026
2.1.1 46 3/31/2026
2.1.0 40 3/31/2026
2.0.0 52 3/30/2026
1.0.2 53 3/29/2026
1.0.1 39 3/29/2026