233 lines
7.6 KiB
C#
233 lines
7.6 KiB
C#
using System;
|
||
using System.Collections.Generic;
|
||
using System.ComponentModel;
|
||
using System.Linq;
|
||
using System.Reflection;
|
||
using System.Web.Mvc;
|
||
|
||
namespace Common
|
||
{
|
||
public class EnumHelper
|
||
{
|
||
/// <summary>
|
||
/// 把枚举的描述和值绑定到DropDownList
|
||
/// </summary>
|
||
/// <param name="enumType"></param>
|
||
/// <returns></returns>
|
||
public static List<SelectListItem> GetCategorySelectList(Type enumType, bool addAll = true, object value = null)
|
||
{
|
||
List<SelectListItem> selectList = new List<SelectListItem>();
|
||
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;
|
||
}
|
||
|
||
/// <summary>
|
||
/// 把枚举的描述和字面值绑定到DropDownList
|
||
/// </summary>
|
||
/// <param name="enumType"></param>
|
||
/// <returns></returns>
|
||
public static List<SelectListItem> GetCategorySelectTextList(Type enumType, bool addAll = true, object value = null)
|
||
{
|
||
List<SelectListItem> selectList = new List<SelectListItem>();
|
||
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;
|
||
}
|
||
|
||
/// <summary>
|
||
///
|
||
/// </summary>
|
||
/// <typeparam name="T"></typeparam>
|
||
/// <param name="strValue"></param>
|
||
/// <returns></returns>
|
||
public static string ConvertToE<T>(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;
|
||
}
|
||
|
||
/// <summary>
|
||
/// 将字符串转换为枚举类型
|
||
/// </summary>
|
||
/// <typeparam name="T"></typeparam>
|
||
/// <param name="strValue"></param>
|
||
/// <returns></returns>
|
||
public static T? ConvertToEnum<T>(string strValue) where T : struct, IConvertible
|
||
{
|
||
T t;
|
||
if (System.Enum.TryParse(strValue, true, out t))
|
||
{
|
||
return t;
|
||
}
|
||
return null;
|
||
}
|
||
|
||
/// <summary>
|
||
/// 根据枚举字面值返回描述文件
|
||
/// </summary>
|
||
/// <typeparam name="T"></typeparam>
|
||
/// <param name="strValue"></param>
|
||
/// <returns></returns>
|
||
public static string GetDesc<T>(object e) where T : struct, IConvertible
|
||
{
|
||
if (e == null)
|
||
{
|
||
return "";
|
||
}
|
||
return GetEnumDescription(e);
|
||
}
|
||
|
||
|
||
/// <summary>
|
||
/// 获取枚举的描述文本
|
||
/// </summary>
|
||
/// <param name="e">枚举成员</param>
|
||
/// <returns></returns>
|
||
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<EnumSelectItem> GetSelectItems<T>() where T : struct
|
||
{
|
||
var pairs = new List<EnumSelectItem>();
|
||
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<DescriptionAttribute>();
|
||
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; }
|
||
}
|
||
}
|