Get int value from enum in C#

前端 未结 28 2472
臣服心动
臣服心动 2020-11-22 04:58

I have a class called Questions (plural). In this class there is an enum called Question (singular) which looks like this.

public e         


        
28条回答
  •  我寻月下人不归
    2020-11-22 05:27

    Following is the extension method

    public static string ToEnumString(this int enumValue)
    {
        var enumString = enumValue.ToString();
        if (Enum.IsDefined(typeof(TEnum), enumValue))
        {
            enumString = ((TEnum) Enum.ToObject(typeof (TEnum), enumValue)).ToString();
        }
        return enumString;
    }
    

提交回复
热议问题