-
Notifications
You must be signed in to change notification settings - Fork 4.8k
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
Add a non-generic version of Enum.TryParse #14083
Comments
Can you help me understand why you'd want the non generic version? What are the scenarios where you have an opaque type you know is an enum and a string and you need to parse it? |
It is mostly for serialization, where the type of the enum is reflected and not fixed in the code upfront. |
I agree I would find that helpful too. If a string is entered by user or otherwise received and the call site expects or looks for particular type of enum, you currently have to try/catch Parse, use reflection or GetNames and search manually, not sure which is worse performance-wise. You especially can't use the generic overloads if you know the nullable version of the enum type. That is perfect use for the suggested TryCatch overload: you get the enum type with Nullable.GetUnderlyingType and you want to parse that enum type or return null when it fails. |
@ellismg are you opposed to having the non-generic version? I'm fine either way. @KrzysztofCwalina @weshaggard any objections to the APIs as proposed? |
No objections. Makes sense. |
agreed. |
I need this. I'm the author of OmniXAML (cross-platform XAML framework) and it would be useful to avoid extra calls like Parsing XAML requieres string values to be converted dynamically to their target types. When I want to convert to a Enum type, I know the type of the Enum and the string representation of the value that may or may not be a correct value, so a call like the propose (non-generic) should be added:
|
I need the same thing. In my use case I'm changing mixed property keys in a file format where they can add unknown keys for their own meta data. I read the property key and cast to the correct enum in a try/catch and only process the known ones. Being able to tryParse with a runtime type would help me out |
Can anyone see issues with this approach?
apart from not supporting flagged enums and I can't get markdown to indent the code :/ |
It does not respect ignoreCase and is not optimal because it compares the value twice. |
Worse, it does not parse flags. (To solve the problem of casing and two evaluations, you could implement the |
Looks good; we should add both, the generic- and the non-generic forms so that |
Should there be a |
Yes, I will update the PR to corert. |
It is the same issue for Enum.ToObject |
This issue can now be tagged as "api-needs-exposed". |
What are the steps to expose an API like this? |
Fix typo in Fedora 26 in 2.0.0 preview 2 release notes. Thanks @omajid !
These are the methods in Enum class related to string parsing.
Apparently Parse is exposed only in non-generic form, while TryParse is only exposed in generic form. This is somewhat inconsistent.
It is simple enough to call a non-generic method in a generic method, but not the other way around. So Parse can be made generic using the following helper. But it is a lot harder to wrap TryParse into TryParse(Type enumType) without reflection calls.
The suggestion is to add a non-generic Enum.TryParse method, the API signature would look like
The text was updated successfully, but these errors were encountered: