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

Case Insensitive String Enum #402

Closed
gigi81 opened this issue Jun 8, 2023 · 1 comment
Closed

Case Insensitive String Enum #402

gigi81 opened this issue Jun 8, 2023 · 1 comment

Comments

@gigi81
Copy link
Contributor

gigi81 commented Jun 8, 2023

Hi,
I would like to create a SmartEnum<TestEnum, string> where the value is string but I would like for the comparison of values to be case insensitive.
So for example if I have:

public sealed class Currency : SmartEnum<Currency, string>
{
    public static readonly Currency USD = new Currency ("US Dollar", "USD");
}

If I do:

Currency.TryFromValue("usd", out var _) //returns false, would like it to return true

Basically I would like for the TryFromValue to be case insensitive or more generally be able to specify a IComparer.
Any suggestion or idea on how to do this?

@gigi81
Copy link
Contributor Author

gigi81 commented Jun 12, 2023

@ardalis if I have a PR for this do you think it has a chance to get merged.
My solution for this would be implementing support for a Equality comparer attribute that you would use like this:

[SmartEnumStringComparer(StringComparison.InvariantCultureIgnoreCase)]
public sealed class Currency : SmartEnum<Currency, string>
{
    public static readonly Currency USD = new Currency ("US Dollar", "USD");
}

let me know what you think. thanks

ardalis added a commit that referenced this issue Aug 11, 2023
* Implemented SmartEnumComparerAttribute<T> with tests

* Aligned to other files formatting

* Updated method names

* Added usage example

---------

Co-authored-by: Steve Smith <steve@kentsmiths.com>
@gigi81 gigi81 closed this as completed Aug 17, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant