SmartEnum
SmartEnum copied to clipboard
Add source generator
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);
}
}
I like it. Do you want to create one and make a PR?
If nobody have requested to work on this, I'd like to volunteer. I don't know if this is the way to go about it as I'm new to contributing
Go for it @wilsonrivera
A few questions, since I'll be taking #293 too
A) would you rather this to be its own package, something like SmartEnum.SourceGenerator
where the user needs to opt-in or,
B) would like it to be implicitly included in the SmartEnum
package, the same way is done with System.Text.Json
, where the source generator is other package and is just included as an analyzer with the aforementioned package?
If goes for option B, would it be ok to include the attributes in the SmartEnum
or would rather they are auto generated too?
Let's see what it looks like all in one, and if it seems too intrusive we can split it. I'm not sure what it's going to look like, yet, until you implement it, which is why I'm uncertain. Ok?
Yeah, sounds good, I'll work on it and get a PR ASAP!
See also https://github.com/ardalis/SmartEnum/pull/356