封装一个自己的EnumHelper类
我们在实际开发中,经常会有对枚举类的一些操作,比如获取枚举值,枚举上的中文名标记等,因此封装了以下的操作类,用于提高开发的效率。
public static class EnumHelper
{
public static Array GetValues(Type enumType)
{
return Enum.GetValues(enumType);
}
public static short GetEnumMaxValue(Type enumType)
{
short num = 0;
foreach (object obj in EnumHelper.GetValues(enumType))
num |= Convert.ToInt16(obj);
return num;
}
public static string GetEnumItemDisplayText(object current)
{
if (current == null)
return string.Empty;
string description = current.ToString();
MemberInfo[] member = current.GetType().GetMember(description);
if (member.Length == 0)
return description;
object[] customAttributes = member[0].GetCustomAttributes(typeof (DescriptionAttribute), false);
if (customAttributes.Length != 0 && customAttributes[0] is DescriptionAttribute)
description = ((DescriptionAttribute) customAttributes[0]).Description;
return description;
}
}
ps:每天进步一点点,将会是一大步。