public class EnumDescription
{
/// <summary>
/// 根据枚举类型返回类型中的所有值,文本及描述
/// </summary>
/// <param name="type"></param>
/// <returns>返回三列数组,第0列为Description,第1列为Value,第2列为Text</returns>
public static List<string[]> GetEnumOpt(Type type)
{
List<string[]> Strs = new List<string[]>();
FieldInfo[] fields = type.GetFields();
for (int i = 1, count = fields.Length; i < count; i++)
{
string[] strEnum = new string[3];
FieldInfo field = fields[i];
//值列
strEnum[1] = ((int)System.Enum.Parse(type, field.Name)).ToString();
//文本列赋值
strEnum[2] = field.Name;
object[] objs = field.GetCustomAttributes(typeof(DescriptionAttribute), false);
if (objs == null || objs.Length == 0)
{
strEnum[0] = field.Name;
}
else
{
DescriptionAttribute da = (DescriptionAttribute)objs[0];
strEnum[0] = da.Description;
}
Strs.Add(strEnum);
}
return Strs;
}
/// <summary>
/// 获取枚举类子项描述信息
/// </summary>
/// <param name="enumSubitem">枚举类子项</param>
public static string GetEnumDescription(object enumSubitem)
{
enumSubitem = (System.Enum)enumSubitem;
string strValue = enumSubitem.ToString();
FieldInfo fieldinfo = enumSubitem.GetType().GetField(strValue);
if (fieldinfo != null)
{
Object[] objs = fieldinfo.GetCustomAttributes(typeof(DescriptionAttribute), false);
if (objs == null || objs.Length == 0)
{
return strValue;
}
else
{
DescriptionAttribute da = (DescriptionAttribute)objs[0];
return da.Description;
}
}
else
{
return "不限";
}
}
}