【转】[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>();

 

posted on 2024-03-23 03:08  z5337  阅读(3)  评论(0编辑  收藏  举报