.net c# enum 枚举 绑定下拉框 DropDownList 获取枚举描述 Description
枚举类:
namespace bhis.egoa.egoa
{
/// <summary>
/// 常用数据
/// </summary>
public class CodeTable
{
#region 操作类别
public enum OperateType
{
Normal = 0,
Add = 1,
Update = 2,
Delete = 3,
Search = 4
}
#endregion
#region 证件类别
[Description("证件类别")]
public enum CertificateType
{
[Description("")]
Empty = 0,
[Description("身份证")]
IdentificationCard = 1,
[Description("护照")]
Passport = 2
}
#endregion
#region 结算方式
[Description("结算方式")]
public enum ClearingWay
{
[Description("")]
Empty = 0,
[Description("现金")]
Cash = 1,
[Description("支票")]
Check = 2
}
#endregion
#region 活跃类型
public enum ActiveType
{
/// <summary>
/// 新知识
/// </summary>
newKnowledge = 1,
/// <summary>
/// 新补充
/// </summary>
newKnowledgeExtend = 2,
/// <summary>
/// 采纳补充
/// </summary>
acceptKnowledgeExtend = 3,
/// <summary>
/// 新评论
/// </summary>
newComment = 4,
/// <summary>
/// 评分
/// </summary>
Card = 5,
/// <summary>
/// 被评分
/// </summary>
BF = 6
}
#endregion
}
}
方法一:
this.ddl_test.DataSource = ListTypeForEnum();
this.ddl_test.DataValueField = "value";
this.ddl_test.DataTextField = "text";
this.ddl_test.DataBind();
/// <summary>
/// 把枚举转换成数组
/// </summary>
/// <returns></returns>
public static IList ListTypeForEnum()
{
ArrayList list = new ArrayList();
foreach (int i in Enum.GetValues(typeof(CodeTable.CertificateType)))
{
//ListItem listitem = new ListItem(Enum.GetName(typeof(CodeTable.CertificateType), i), i.ToString());
ListItem listitem = new ListItem(GetDescription((CodeTable.CertificateType)i), i.ToString());
list.Add(listitem);
}
return list;
}
/// <summary>
/// 返回枚举项的描述信息。
/// </summary>
/// <param name="value">要获取描述信息的枚举项。</param>
/// <returns>枚举想的描述信息。</returns>
public static string GetDescription(Enum value)
{
Type enumType = value.GetType();
// 获取枚举常数名称。
string name = Enum.GetName(enumType, value);
if (name != null)
{
// 获取枚举字段。
FieldInfo fieldInfo = enumType.GetField(name);
if (fieldInfo != null)
{
// 获取描述的属性。
DescriptionAttribute attr = Attribute.GetCustomAttribute(fieldInfo,
typeof(DescriptionAttribute), false) as DescriptionAttribute;
if (attr != null)
{
return attr.Description;
}
}
}
return null;
}
方法二:
BindDropDownList(this.ddl_test, typeof(CodeTable.ActiveType));
/// <summary>
/// 绑定下拉选项
/// </summary>
/// <param name="ddlObject">下拉框</param>
/// <param name="obj">CodeTable</param>
public static void BindDropDownList(DropDownList ddlObject, Type obj)
{
ddlObject.DataSource = EnumDescription.GetFieldTexts(obj);
ddlObject.DataValueField = "EnumValue";
ddlObject.DataTextField = "EnumDisplayText";
ddlObject.DataBind();
}
(新加的类如下 :)
using System;
using System.Reflection;
using System.Collections;
namespace bhis.egoa.egoa
{
[AttributeUsage(AttributeTargets.Field | AttributeTargets.Enum)]
public class EnumDescription : Attribute
{
// Fields
private static Hashtable cachedEnum = new Hashtable();
private string enumDisplayText;
private int enumRank;
private FieldInfo fieldIno;
// Methods
public EnumDescription(string enumDisplayText)
: this(enumDisplayText, 5)
{
}
public EnumDescription(string enumDisplayText, int enumRank)
{
this.enumDisplayText = enumDisplayText;
this.enumRank = enumRank;
}
public static string GetEnumText(Type enumType)
{
EnumDescription[] customAttributes = (EnumDescription[])enumType.GetCustomAttributes(typeof(EnumDescription), false);
if (customAttributes.Length != 1)
{
return string.Empty;
}
return customAttributes[0].EnumDisplayText;
}
public static string GetFieldText(object enumValue)
{
EnumDescription[] fieldTexts = GetFieldTexts(enumValue.GetType(), SortType.Default);
foreach (EnumDescription description in fieldTexts)
{
if (description.fieldIno.Name == enumValue.ToString())
{
return description.EnumDisplayText;
}
}
return string.Empty;
}
public static EnumDescription[] GetFieldTexts(Type enumType)
{
return GetFieldTexts(enumType, SortType.Default);
}
public static EnumDescription[] GetFieldTexts(Type enumType, SortType sortType)
{
EnumDescription[] descriptionArray = null;
if (!cachedEnum.Contains(enumType.FullName))
{
FieldInfo[] fields = enumType.GetFields();
ArrayList list = new ArrayList();
foreach (FieldInfo info in fields)
{
object[] customAttributes = info.GetCustomAttributes(typeof(EnumDescription), false);
if (customAttributes.Length == 1)
{
((EnumDescription)customAttributes[0]).fieldIno = info;
list.Add(customAttributes[0]);
}
}
cachedEnum.Add(enumType.FullName, (EnumDescription[])list.ToArray(typeof(EnumDescription)));
}
descriptionArray = (EnumDescription[])cachedEnum[enumType.FullName];
if (descriptionArray.Length <= 0)
{
throw new NotSupportedException("枚举类型[" + enumType.Name + "]未定义属性EnumValueDescription");
}
for (int i = 0; i < descriptionArray.Length; i++)
{
if (sortType == SortType.Default)
{
return descriptionArray;
}
for (int j = i; j < descriptionArray.Length; j++)
{
bool flag = false;
switch (sortType)
{
case SortType.DisplayText:
if (string.Compare(descriptionArray[i].EnumDisplayText, descriptionArray[j].EnumDisplayText) > 0)
{
flag = true;
}
break;
case SortType.Rank:
if (descriptionArray[i].EnumRank > descriptionArray[j].EnumRank)
{
flag = true;
}
break;
}
if (flag)
{
EnumDescription description = descriptionArray[i];
descriptionArray[i] = descriptionArray[j];
descriptionArray[j] = description;
}
}
}
return descriptionArray;
}
// Properties
public string EnumDisplayText
{
get
{
return this.enumDisplayText;
}
}
public int EnumRank
{
get
{
return this.enumRank;
}
}
public int EnumValue
{
get
{
return (int)this.fieldIno.GetValue(null);
}
}
public string FieldName
{
get
{
return this.fieldIno.Name;
}
}
// Nested Types
public enum SortType
{
Default,
DisplayText,
Rank
}
}
}