How to get enum value by string or int

后端 未结 10 1268
慢半拍i
慢半拍i 2020-12-07 12:43

How can I get the enum value if I have the enum string or enum int value. eg: If i have an enum as follows:

public enum TestEnum
{
    Value1 = 1,
    Value         


        
10条回答
  •  清歌不尽
    2020-12-07 13:32

    Try something like this

      public static TestEnum GetMyEnum(this string title)
            {    
                EnumBookType st;
                Enum.TryParse(title, out st);
                return st;          
             }
    

    So you can do

    TestEnum en = "Value1".GetMyEnum();
    

提交回复
热议问题