How to use generic Tryparse with Enum?

后端 未结 5 1312
时光说笑
时光说笑 2020-12-05 06:56

I\'m trying to build generic function that get from user string and try to parse it to Enum valuse like this:

private Enum getEnumStringEnumType(Type i_EnumT         


        
5条回答
  •  死守一世寂寞
    2020-12-05 07:35

    Enum.TryParse is a generic method, which means that its generic type parameters have to be known at compile time. This in turn means that yes, you do have to declare resultInputType as a specific enum type for the code to compile.

    If you think on it, the original code is a bit too optimistic: nowhere does it say which enum type should be checked for a member with name equal to userInputString. How could TryParse work without this information?

提交回复
热议问题