C# numeric enum value as string

前端 未结 8 1190
隐瞒了意图╮
隐瞒了意图╮ 2020-12-07 18:34

I have the following enum:

public enum Urgency {
    VeryHigh = 1,
    High     = 2,
    Routine  = 4
}

I can fetch an enum \"value

8条回答
  •  盖世英雄少女心
    2020-12-07 19:15

    If you wanted, you could make the extension method work for all enums:

    public static string ToValueString(this Enum enumValue) 
    {
        if (enumValue.GetType().GetEnumUnderlyingType() == typeof(int))
            return ((int)(object)enumValue).ToString();
        else if (enumValue.GetType().GetEnumUnderlyingType() == typeof(byte))
            return ((byte)(object)enumValue).ToString();
        ... 
    }        
    

提交回复
热议问题