【轉】[C#] EnumHelper

public static class EnumHelper
{
    public static Array GetValues(Type enumType)
    {
        return Enum.GetValues(enumType);
    }

    public static short GetEnumMaxValue(Type enumType)
    {
        short num = 0;
        Array values = GetValues(enumType);
        foreach (object value in values)
        {
            num |= Convert.ToInt16(value);
        }
        return num;
    }

    public static string GetEnumItemDisplayText(object current)
    {
        if (current == null)
        {
            return string.Empty;
        }
        string text = current.ToString();
        MemberInfo[] member = current.GetType().GetMember(text);
        if (member.Length == 0)
        {
            return text;
        }
        object[] customAttributes = member[0].GetCustomAttributes(typeof(DescriptionAttribute), false);
        if (customAttributes.Length > 0 && customAttributes[0] is DescriptionAttribute)
        {
            text = ((DescriptionAttribute)customAttributes[0]).Description;
        }
        return text;
    }
}

 調用方式:

return EnumHelper.GetValues(typeof(Alignment)).Cast<Alignment>();

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章