how to check if string value is in the Enum list?

前端 未结 7 867
深忆病人
深忆病人 2021-01-30 05:15

In my query string, I have an age variable ?age=New_Born.

Is there a way I can check if this string value New_Born is in my Enum list

7条回答
  •  醉梦人生
    2021-01-30 05:35

    I've got a handy extension method that uses TryParse, as IsDefined is case-sensitive.

    public static bool IsParsable(this string value) where T : struct
    {
        return Enum.TryParse(value, true, out _);
    }
    

提交回复
热议问题