Type-safe source-generated alternative to C# enum
inspired by Kotlin enum class
This library contains source generator that creates class
for specified enum
.
These classes contain similar functionality as original enum, but behave as ordinal class
- Add reference to Nuget package
- .NET CLI
dotnet add package EnumClass --version 1.2.0
- Package Manager
Install-Package EnumClass -Version 1.2.0
- PackageReference
<PackageReference Include="EnumClass" Version="1.2.0" />
- Specify Analyzer property in
.csproj
<PackageReference Include="EnumClass" Version="1.2.0" OutputItemType="Analyzer"/>
Add [EnumClass]
attribute to enum
That is all! Corresponding class will be generated in namespace as your enum, but prefixed with EnumClass
Example
using EnumClass.Attributes;
namespace Domain
{
[EnumClass]
public enum PetKind
{
Cat,
Dog
}
namespace EnumClass
{
public partial abstract class PetKind
{
public partial class CatEnumValue
{
public void SayMeow()
{
System.Console.WriteLine("Meow!");
}
}
}
}
}
All ToString()
are generated at compile time.
By default, they equal to name of corresponding member.
Console.WriteLine(EnumClass.PetKind.Cat.ToString() == "Cat");
// Output: true
If you want to override default value - use [EnumMemberInfo(StringValue = "")]
attribute
namespace Domain;
[EnumClass]
public enum PetKind
{
[EnumMemberInfo(StringValue = "Kitten")]
Cat,
Dog
}
// -------------
Console.WriteLine(EnumClass.PetKind.Cat.ToString());
// Output: Kitten
All classes have overriden cast operator to original enum value
Console.WriteLine(((PetKind)EnumClass.PetKind.Cat) == PetKind.Cat);
// Output: true
All classes have overriden cast to int
Console.WriteLine(((int)EnumClass.PetKind.Cat) == ((int)PetKind.Cat));
// Output: true
Generated classes implement IEquatable<>
both for enum class and original enum.
Thus, has methods Equals(EnumClass)
and Equals(OrignalEnum)
Console.WriteLine(EnumClass.PetKind.Cat.Equals(EnumClass.PetKind.Cat)); // Calls Equals(EnumClass.PetKind)
Console.WriteLine(EnumClass.PetKind.Cat.Equals(EnumClass.PetKind.Dog)); // Calls Equals(EnumClass.PetKind)
Console.WriteLine(EnumClass.PetKind.Cat.Equals(PetKind.Cat)); // Calls Equals(PetKind)
Console.WriteLine(EnumClass.PetKind.Cat.Equals(PetKind.Dog)); // Calls Equals(PetKind)
// Output: true
// false
// true
// false
P.S. and of course Equals(object?)
Instead of writing switch
every time, a fimily
of switch function is generated.
They accepts both Action
and Func
with enum class at first argument and optional additional arguments.
E.g.
Func<int>
var cat = EnumClass.PetKind.Cat;
var value = cat.Switch(1,
(cat, i) => i + 1,
(dog, i) => i * 100);
Console.WriteLine(value);
// Output: 2
Action
var dog = EnumClass.PetKind.Dog;
dog.Switch(
cat => cat.SayMeow(),
dog => Console.WriteLine("Oh, it is puppy!"));
// Output: Oh, it is puppy!
There is static function bool EnumClass.TryParse(string value, out EnumClass enumClass)
for parsing values from raw enums.
This function accepts strings with and without enum name: PetKind.Cat and Cat will be parsed into Cat value.
if (EnumClass.PetKind.TryParse("Cat", out var cat)
{
Console.WriteLine($"This is cat!! {cat}");
}
else
{
Console.WriteLine("This is not cat");
}
For more examples checkout samples folder
It uses incremental generator instead of source generator. This implies better performance in comparison
By default generated class contained in the same namespace as original enum + ".EnumClass" suffix.
You can manually set target namespace in Namespace
property of [EnumClass]
attribute.
using EnumClass.Attributes;
namespace Test;
[EnumClass(Namespace = "Domain")]
public enum SampleEnum
{
First,
Second
}
//------------------
using Domain;
Console.WriteLine(SampleEnum.First);
If you do not have access to enum source code directly, you can generate enum class
for enum in external assembly.
For this use [ExternalEnumClass]
attribute.
// External assembly
namespace Logic;
public enum Word
{
Single,
Double,
Triple
}
// Our assembly
using EnumClass.Attributes;
using Logic;
[assembly: ExternalEnumClass(typeof(Word), Namespace = "Another")]
namespace Another;
public partial class Word
{
public abstract int WordsCount { get; }
public partial class SingleEnumValue
{
public override int WordsCount => 1;
}
public partial class DoubleEnumValue
{
public override int WordsCount => 2;
}
public partial class TripleEnumValue
{
public override int WordsCount => 3;
}
}
In the current implementation, static enum class field names have the same names as members of the original enum.
E.g.
[EnumClass]
public enum TokenType
{
TokenType
}
will generate approximately the following code
public class TokenType
{
public static TokenTypeEnumValue TokenType = new();
}
The project at an early stage of life. I'm sure there are lots of hidden bugs, so be cautious using it in production. In production may be better for now to use SmartEnum
If you have an idea on how to improve the project or have found a bug, create an issue on GitHub
If you want to see the continuation of the project, give it a star!