EnumToClass 1.0.5

There is a newer version of this package available.
See the version list below for details.
dotnet add package EnumToClass --version 1.0.5                
NuGet\Install-Package EnumToClass -Version 1.0.5                
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.5">
  <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.5                
#r "nuget: EnumToClass, 1.0.5"                
#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.5

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

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>]
internal sealed partial class TestElementClass
{
}

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

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

The generator will emit the following partial classes:

TestElementClass_Constants.g.cs:

//<auto-generated />     
using System.Collections.Generic;
using System.Collections.ObjectModel;
#nullable enable

namespace EnumToClass.Tests.Integration
{
    internal partial class TestElementClass            
    {
        private static readonly ReadOnlyDictionary<string, TestElementClass> ValueByNameMap = new ReadOnlyDictionary<string, TestElementClass>(new Dictionary<string, TestElementClass>()
        {
            ["None"] = new TestElementClass(EnumToClass.Tests.Integration.TestElements.None),
            ["Element1"] = new TestElementClass(EnumToClass.Tests.Integration.TestElements.Element1),
            ["Element2"] = new TestElementClass(EnumToClass.Tests.Integration.TestElements.Element2),
            ["Element3"] = new TestElementClass(EnumToClass.Tests.Integration.TestElements.Element3)
        });                        
        public const string None = "None";
        public const string Element1 = "Element1";
        public const string Element2 = "Element2";
        public const string Element3 = "Element3";
    }
}

and TestElementClass_Constructors.g.cs:

//<auto-generated />                        
using System.Collections.Generic;
#nullable enable
namespace EnumToClass.Tests.Integration
{
    internal partial class TestElementClass            
    {                    
        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 override bool Equals(object? obj)
        {
            return obj is TestElementClass element
                ? element.Value == Value
                : base.Equals(obj);
        }
        public override int GetHashCode() => Value.GetHashCode();
       
        public static IReadOnlyCollection<TestElementClass> GetAll() => ValueByNameMap.Values;
            
        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());
        public static implicit operator int(TestElementClass value) => (int) value.Value;
    }
}

Change Log

Version 1.0.5

  1. Add documentation comments based on DescriptionAttribute value.
  2. Fix full enum type name for external enums.
  3. Fix documentation comments for external enums.

Version 1.0.4

  1. Add documentation comments to generated const values based on documentation comments for the enum element

Version 1.0.3

  1. Fix method names in generator.
  2. Replace reflection generated to generator generated values dictionary.
  3. Add GetAll() static function to the class with EnumToClassAttribute attribute.
  4. Add implicit operator to convert enum type value to underlying enum type.
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 103 10/3/2024
1.0.5 85 9/23/2024
1.0.4 84 9/23/2024
1.0.3 79 9/20/2024
1.0.2 121 9/14/2024