Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generator for enum values #4

Closed
CollinAlpert opened this issue Jan 19, 2022 · 4 comments
Closed

Generator for enum values #4

CollinAlpert opened this issue Jan 19, 2022 · 4 comments
Labels
participation wanted This requires community input/opinions.

Comments

@CollinAlpert
Copy link
Owner

CollinAlpert commented Jan 19, 2022

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.

@CollinAlpert CollinAlpert added the participation wanted This requires community input/opinions. label Jan 19, 2022
@CollinAlpert CollinAlpert pinned this issue Feb 25, 2022
@truepele
Copy link

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

@CollinAlpert
Copy link
Owner Author

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?

@candoumbe
Copy link

@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

@CollinAlpert
Copy link
Owner Author

Available in v2.3.0

@CollinAlpert CollinAlpert unpinned this issue Dec 28, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
participation wanted This requires community input/opinions.
Projects
None yet
Development

No branches or pull requests

3 participants