CollinAlpert/Lombok.NET

Generator for enum values

Closed this issue · 4 comments

I am working on a generator which is supposed to generate a static property or method called Values which would return an enum's members. This would alleviate the need of the reflection-based Enum.GetValues() approach and would also be statically typed.

However, I am running into design issues. Enums can't be partial which means the source generator couldn't simply plug in and add a static property. I also thought about extension method, but this would require a specific enum field and I want to add a static property on the enum type.

The best I can currently come up with is the following:

public enum Mood
{
  Happy,
  Sad,
  Mad
}

The generator would then produce the following code:

public static class MoodEnumeration
{
  public static Mood[] Values => new [] { Mood.Happy, Mood.Sad, Mood.Mad };
}

But from a users's standpoint, that design is less than ideal. Please leave a comment here if you are interested in this feature and if so, what design would work best for you.

Hi @CollinAlpert
First, thank you for the amazing library! Keep up the awesome work!
For the issue - have you considered using “enum classes” instead of pure enums? This is a “better practice”, but people usually avoid it because of the boilerplate code it involves. Would be awesome if this library did the job for enum classes generation.
https://learn.microsoft.com/en-us/dotnet/architecture/microservices/microservice-ddd-cqrs-patterns/enumeration-classes-over-enum-types

Hi! I'm glad you enjoy the library. And thanks for chiming in.

I can definitely add a generator for this. It could consider public static readonly fields in a partial class with a specific attribute and then generate a Values property based on these fields. In general, what do you think about this procedure?

And what would a fitting attribute name be?

@CollinAlpert
I came across this library and I must say it's a must !!!

Hi! I'm glad you enjoy the library. And thanks for chiming in.

I can definitely add a generator for this. It could consider public static readonly fields in a partial class with a specific attribute and then generate a Values property based on these fields. In general, what do you think about this procedure?

And what would a fitting attribute name be?

The attribute name could be EnumerationAttribute

Available in v2.3.0