ardalis/SmartEnum

Add source generator

Open

#259 opened on Apr 18, 2022

View on GitHub
 (7 comments) (5 reactions) (0 assignees)C# (185 forks)github user discovery
enhancementgood first issuehelp wanted

Repository metrics

Stars
 (2,420 stars)
PR merge metrics
 (No merged PRs in 30d)

Description

This could be improved further with source generators:

Allowing you to just decorate a partial class like so:

using Ardalis.SmartEnum;

[SmartEnum]
public sealed partial class TestEnum
{
    [EnumMember]
    public static readonly TestEnum One;

    [EnumMember]
    public static readonly TestEnum Two;

    [EnumMember]
    public static readonly TestEnum Three;
}

Generated code:

using Ardalis.SmartEnum;

sealed partial class TestEnum : SmartEnum<TestEnum>
{
    private TestEnum(string name, int value) : base(name, value)
    {
        One = new TestEnum(nameof(One), 1);
        Two = new TestEnum(nameof(Two), 2);
        Three = new TestEnum(nameof(Three), 3);
    }
}

Contributor guide