How to use generic Tryparse with Enum?

后端 未结 5 1311
时光说笑
时光说笑 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:27

    String Extension Method

    public TEnum ToEnum(this string value, TEnum defaultValue){
    
    if (string.IsNullOrEmpty(value))return defaultValue;
    
    return Enum.Parse(typeof(TEnum), value, true);}
    

提交回复
热议问题