EnumToClass 1.0.2

There is a newer version of this package available.
See the version list below for details.
dotnet add package EnumToClass --version 1.0.2                
NuGet\Install-Package EnumToClass -Version 1.0.2                
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="EnumToClass" Version="1.0.2">
  <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.
paket add EnumToClass --version 1.0.2                
#r "nuget: EnumToClass, 1.0.2"                
#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 EnumToClass as a Cake Addin
#addin nuget:?package=EnumToClass&version=1.0.2

// Install EnumToClass as a Cake Tool
#tool nuget:?package=EnumToClass&version=1.0.2                

EnumToClass Generatror

Enum to class generator generates smart enum class containing constant strings equivalent to enum names. The class decorated with EnumToClassAttribute becomes closed type with private constructor and readonly properties.

Usage

namespace EnumToClass.Tests.Integration;

public enum TestElements
{
    None,
    Element1,
    Element2,
    Element3
}

[EnumToClass<TestElements>]
public sealed partial class TestElementClass
{
}

[EnumToClass<TestElements>]
public sealed partial class TestElementRecord
{
}

The default element of the enum will be generated with flag IsEmpty set to true.

The generator will emit the following partial classes:

namespace EnumToClass.Tests.Integration
{
    public partial class TestElementClass
    {
        public const string None = "None";
        public const string Element1 = "Element1";
        public const string Element2 = "Element2";
        public const string Element3 = "Element3";
    }
}

and

//<auto-generated />            
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;

namespace EnumToClass.Tests.Integration
{
    public partial class TestElementClass
    {
        private static readonly ReadOnlyDictionary<string, TestElementClass> ValueByNameMap = Enum
            .GetValues<Integration.TestElements>()
            .Select(p => new TestElementClass(p))
            .ToDictionary(p => p.Name, p => p)
            .AsReadOnly();
        private TestElementClass(Integration.TestElements value)
        {
            Name = value.ToString();
            IsEmpty = value == default(Integration.TestElements);
            Value = value;
        }
        public string Name { get; }
        public Integration.TestElements Value { get; }
        public bool IsEmpty { get; }                            

        public static TestElementClass Empty { get; } = new TestElementClass(default(Integration.TestElements));
        public override string ToString() => Name;

        public static IEnumerable<TestElementClass> GetByName(IEnumerable<string> names)
            => names.Select(GetByName).Where(p => p.IsEmpty == false);

        public static TestElementClass GetByName(string name)
        {
            return ValueByNameMap.TryGetValue(name, out var value)
                ? value
                : Empty;
        }
        public static implicit operator TestElementClass(string name) => GetByName(name);
        public static implicit operator string(TestElementClass value) => value.Name;
        public static implicit operator Integration.TestElements(TestElementClass value) => value.Value;
        public static implicit operator TestElementClass(Integration.TestElements value) => GetByName(value.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
1.0.6 111 10/3/2024
1.0.5 89 9/23/2024
1.0.4 91 9/23/2024
1.0.3 83 9/20/2024
1.0.2 128 9/14/2024