using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Reflection;
using System.Web.Mvc;
namespace Common
{
public class EnumHelper
{
///
/// 把枚举的描述和值绑定到DropDownList
///
///
///
public static List GetCategorySelectList(Type enumType, bool addAll = true, object value = null)
{
List selectList = new List();
if (addAll)
{
selectList.Add(value == null
? new SelectListItem { Text = "--请选择--", Value = "", Selected = true }
: new SelectListItem { Text = "--请选择--", Value = "" });
}
foreach (object e in System.Enum.GetValues(enumType))
{
if (value != null && ((int)e) == (decimal)value)
{
selectList.Add(new SelectListItem
{
Text = GetEnumDescription(e),
Value = ((int)e).ToString(),
Selected = true
});
}
else
{
selectList.Add(new SelectListItem { Text = GetEnumDescription(e), Value = ((int)e).ToString() });
}
}
return selectList;
}
///
/// 把枚举的描述和字面值绑定到DropDownList
///
///
///
public static List GetCategorySelectTextList(Type enumType, bool addAll = true, object value = null)
{
List selectList = new List();
if (addAll)
{
selectList.Add(value == null
? new SelectListItem { Text = "--请选择--", Value = "", Selected = true }
: new SelectListItem { Text = "--请选择--", Value = "" });
}
foreach (object e in Enum.GetValues(enumType))
{
if (value != null && e.ToString() == value.ToString())
{
selectList.Add(new SelectListItem
{
Text = GetEnumDescription(e),
Value = e.ToString(),
Selected = true
});
}
else
{
selectList.Add(new SelectListItem { Text = GetEnumDescription(e), Value = e.ToString() });
}
}
return selectList;
}
///
///
///
///
///
///
public static string ConvertToE(string strValue) where T : struct, IConvertible
{
if (!string.IsNullOrEmpty(strValue))
{
T t;
return System.Enum.TryParse(strValue, true, out t) ? t.ToString() : "";
}
return string.Empty;
}
///
/// 将字符串转换为枚举类型
///
///
///
///
public static T? ConvertToEnum(string strValue) where T : struct, IConvertible
{
T t;
if (System.Enum.TryParse(strValue, true, out t))
{
return t;
}
return null;
}
///
/// 根据枚举字面值返回描述文件
///
///
///
///
public static string GetDesc(object e) where T : struct, IConvertible
{
if (e == null)
{
return "";
}
return GetEnumDescription(e);
}
///
/// 获取枚举的描述文本
///
/// 枚举成员
///
public static string GetEnumDescription(object e)
{
//获取字段信息
System.Reflection.FieldInfo[] ms = e.GetType().GetFields();
//Type t = e.GetType();
foreach (System.Reflection.FieldInfo f in ms)
{
//判断名称是否相等
if (f.Name != e.ToString()) continue;
//反射出自定义属性
foreach (Attribute attr in f.GetCustomAttributes(true))
{
//类型转换找到一个Description,用Description作为成员名称
System.ComponentModel.DescriptionAttribute dscript = attr as System.ComponentModel.DescriptionAttribute;
if (dscript != null)
return dscript.Description;
}
}
//如果没有检测到合适的注释,则用默认名称
return e.ToString();
}
public static string GetEnumDesc(Enum en)
{
Type type = en.GetType();
MemberInfo[] memInfo = type.GetMember(en.ToString()); if (memInfo != null && memInfo.Length > 0)
{
object[] attrs = memInfo[0].GetCustomAttributes(typeof(System.ComponentModel.DataAnnotations.DisplayAttribute), false); if (attrs != null && attrs.Length > 0) return ((System.ComponentModel.DataAnnotations.DisplayAttribute)attrs[0]).Name;
}
return en.ToString();
}
public static List GetSelectItems() where T : struct
{
var pairs = new List();
var ttype = typeof(T);
if (!ttype.IsEnum)
{
throw new Exception("需传入枚举类型");
}
var fs = ttype.GetFields();
var arr = Enum.GetValues(ttype);
for (var i = 0; i < arr.Length; i++)
{
var obj = arr.GetValue(i);
var val = (int)arr.GetValue(i);
var description = GetDescription(fs, obj, val);
pairs.Add(new EnumSelectItem
{
Key = obj.ToString(),
Value = val,
Description = description
});
}
return pairs.Where(w => w.Key != "None").ToList();
}
private static string GetDescription(FieldInfo[] fs, object obj, int val)
{
foreach (var tf in fs)
{
if (tf.FieldType == typeof(int))
{
continue;
}
if ((int)tf.GetValue(obj) != val)
{
continue;
}
var attr = tf.GetCustomAttribute();
if (attr != null)
{
return attr.Description;
}
else
{
return obj.ToString();
}
}
return "";
}
}
public class EnumSelectItem
{
public string Key { get; set; }
public int Value { get; set; }
public string Description { get; set; }
}
}