You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
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?
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.
I need this. I'm the author of OmniXAML (cross-platform XAML framework) and it would be useful to avoid extra calls like Enum.IsDefined
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
Worse, it does not parse flags.
Edit: sorry, missed you mentioned that already - that is pretty severe limitation though
(To solve the problem of casing and two evaluations, you could implement the IsDefined method yourself using case insensitive index search on Enum.GetNames() and using that index into Enum.GetValues())
Activity
ellismg commentedon Feb 8, 2015
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?
yufeih commentedon Feb 8, 2015
It is mostly for serialization, where the type of the enum is reflected and not fixed in the code upfront.
miloush commentedon Jul 31, 2015
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.
terrajobst commentedon Jan 5, 2016
@ellismg are you opposed to having the non-generic version? I'm fine either way.
@KrzysztofCwalina @weshaggard any objections to the APIs as proposed?
KrzysztofCwalina commentedon Jan 5, 2016
No objections. Makes sense.
weshaggard commentedon Jan 6, 2016
agreed.
SuperJMN commentedon Jan 8, 2016
I need this. I'm the author of OmniXAML (cross-platform XAML framework) and it would be useful to avoid extra calls like
Enum.IsDefinedParsing 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:
Enum.TryParse(Type enumType, string value)fruitbatinshades commentedon Jul 1, 2016
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
fruitbatinshades commentedon Jul 1, 2016
Can anyone see issues with this approach?
public static bool TryParseEnum(this string value, Type enumType,bool ignoreCase, out Enum enumValue) { if (Enum.IsDefined(enumType, value)) { enumValue = (Enum)Enum.Parse(enumType, value, ignoreCase); return true; } enumValue = null; return false; }apart from not supporting flagged enums and I can't get markdown to indent the code :/
yufeih commentedon Jul 1, 2016
It does not respect ignoreCase and is not optimal because it compares the value twice.
miloush commentedon Jul 1, 2016
Worse, it does not parse flags.
Edit: sorry, missed you mentioned that already - that is pretty severe limitation though
(To solve the problem of casing and two evaluations, you could implement the
IsDefinedmethod yourself using case insensitive index search onEnum.GetNames()and using that index intoEnum.GetValues())terrajobst commentedon Sep 6, 2016
Looks good; we should add both, the generic- and the non-generic forms so that
ParseandTryParsehave both versions.justinvp commentedon Sep 8, 2016
Should there be a
where TEnum : structconstraint on the proposedParse<TEnum>to match the existingTryParse<TEnum>?yufeih commentedon Sep 8, 2016
Yes, I will update the PR to corert.
magol commentedon Sep 8, 2016
It is the same issue for Enum.ToObject
justinvp commentedon Sep 15, 2016
This issue can now be tagged as "api-needs-exposed".
yufeih commentedon Sep 26, 2016
What are the steps to expose an API like this?
Merge pull request #692 from omajid/master