EnumToClass 1.0.4
There is a newer version of this package available.
See the version list below for details.
See the version list below for details.
dotnet add package EnumToClass --version 1.0.4
NuGet\Install-Package EnumToClass -Version 1.0.4
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.4"> <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.4
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: EnumToClass, 1.0.4"
#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.4 // Install EnumToClass as a Cake Tool #tool nuget:?package=EnumToClass&version=1.0.4
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
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.4
- Add documentation comments to generated const values based on documentation comments for the enum element
Version 1.0.3
- Fix method names in generator.
- Replace reflection generated to generator generated values dictionary.
- Add GetAll() static function to the class with
EnumToClassAttribute
attribute. - 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.