Skip to content

Add a non-generic version of Enum.TryParse #14083

Closed

Description

@yufeih

These are the methods in Enum class related to string parsing.

public static object Parse(Type enumType, string value);
public static object Parse(Type enumType, string value, bool ignoreCase);

public static bool TryParse<TEnum>(string value, out TEnum result) 
where TEnum : struct;
public static bool TryParse<TEnum>(string value, bool ignoreCase, out TEnum result) 
where TEnum : struct;

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.

public static TEnum Parse<TEnum>(string value, bool ignoreCase)
{
    return Enum.Parse(typeof(TEnum), value, ignoreCase);
}

The suggestion is to add a non-generic Enum.TryParse method, the API signature would look like

public static bool TryParse(Type enumType, string value, out object result) 
public static bool TryParse(Type enumType, string value, bool ignoreCase, out object result) 

Activity

  1. ellismg commented on Feb 8, 2015

    @ellismg
    Contributor

    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?

  2. yufeih commented on Feb 8, 2015

    @yufeih
    ContributorAuthor

    It is mostly for serialization, where the type of the enum is reflected and not fixed in the code upfront.

  3. miloush commented on Jul 31, 2015

    @miloush
    Contributor

    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.

  4. terrajobst commented on Jan 5, 2016

    @terrajobst
    Contributor

    @ellismg are you opposed to having the non-generic version? I'm fine either way.

    @KrzysztofCwalina @weshaggard any objections to the APIs as proposed?

  5. KrzysztofCwalina commented on Jan 5, 2016

    @KrzysztofCwalina
    Member

    No objections. Makes sense.

  6. weshaggard commented on Jan 6, 2016

    @weshaggard
    Member

    agreed.

  7. SuperJMN commented on Jan 8, 2016

    @SuperJMN

    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:

    Enum.TryParse(Type enumType, string value)

  8. fruitbatinshades commented on Jul 1, 2016

    @fruitbatinshades

    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

  9. fruitbatinshades commented on Jul 1, 2016

    @fruitbatinshades

    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 :/

  10. yufeih commented on Jul 1, 2016

    @yufeih
    ContributorAuthor

    It does not respect ignoreCase and is not optimal because it compares the value twice.

  11. miloush commented on Jul 1, 2016

    @miloush
    Contributor

    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())

  12. terrajobst commented on Sep 6, 2016

    @terrajobst
    Contributor

    Looks good; we should add both, the generic- and the non-generic forms so that Parse and TryParse have both versions.

  13. justinvp commented on Sep 8, 2016

    @justinvp
    Contributor

    Should there be a where TEnum : struct constraint on the proposed Parse<TEnum> to match the existing TryParse<TEnum>?

  14. yufeih commented on Sep 8, 2016

    @yufeih
    ContributorAuthor

    Yes, I will update the PR to corert.

  15. magol commented on Sep 8, 2016

    @magol

    It is the same issue for Enum.ToObject

  16. justinvp commented on Sep 15, 2016

    @justinvp
    Contributor

    This issue can now be tagged as "api-needs-exposed".

  17. yufeih commented on Sep 26, 2016

    @yufeih
    ContributorAuthor

    What are the steps to expose an API like this?

  18. transferred this issue fromdotnet/corefxon Jan 31, 2020
  19. added this to the 2.0.0 milestone on Jan 31, 2020
  20. ghost locked as resolved and limited conversation to collaborators on Jan 7, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    api-approvedarea-System.Runtime

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions