1.常见的枚举是这样的:
public enum AwardsType, { World = 1, Country = 2, Provinces=3, School = 4 };
如果我们要获取额外的信息,比如 world的中文显示,显然上面的写法是不能满足我们的,自己研究了一下,决定给枚举加上自定义属性。加上自定义属性的枚举如下:
[EnumDescription("荣誉奖项")] public enum AwardsType { [EnumDescription("世界级")] World = 1, [EnumDescription("国家级")] Country = 2, [EnumDescription("省市级")] Provinces=3, [EnumDescription("校级")] School = 4 };
注意EnumDescription是自己写的一个类,下面会贴上源码。
2.代码访问:
//0、获得枚举类型的文字描述string txt = EnumDescription.GetEnumText(typeof(OrderStateEnum));
//1、获得某个枚举值的文字描述:
string txt = EnumDescription.GetFieldText(OrderStateEnum.Processing)
//2、获得枚举中各个字段的定义数组:
EnumDescription[] des = EnumDescription.GetFieldTexts(typeof(OrderStateEnum))//3、如果需要排序,通过他的另一个重载
public static EnumDescription[] GetFieldTexts( Type enumType, SortType sortType )
//4、绑定到下拉框:
ddlTypeID.DataSource =EnumDescription.GetFieldTexts(typeof(DataHelper.AwardsType));
ddlTypeID.DataTextField = "EnumDisplayText";
ddlTypeID.DataValueField = "EnumValue";
ddlTypeID.DataBind();
3、EnumDescription源码
/// <summary> /// 把枚举值按照指定的文本显示 /// <remarks> /// 一般通过枚举值的ToString()可以得到变量的文本, /// 但是有时候需要的到与之对应的更充分的文本, /// 这个类帮助达到此目的 /// </remarks> /// </summary> /// <example> /// [EnumDescription("中文数字")] /// enum MyEnum /// { /// [EnumDescription("数字一")] /// One = 1, /// /// [EnumDescription("数字二")] /// Two, /// /// [EnumDescription("数字三")] /// Three /// } /// EnumDescription.GetEnumText(typeof(MyEnum)); /// EnumDescription.GetFieldText(MyEnum.Two); /// EnumDescription.GetFieldTexts(typeof(MyEnum)); /// </example> [AttributeUsage(AttributeTargets.Field | AttributeTargets.Enum)] public class EnumDescription : Attribute { private string enumDisplayText; private int enumRank; private FieldInfo fieldIno;</span><span style="color: #808080;">///</span> <span style="color: #808080;"><summary></span></br> <span style="color: #808080;">///</span><span style="color: #008000;"> 描述枚举值</br> </span><span style="color: #808080;">///</span> <span style="color: #808080;"></summary></span></br> <span style="color: #808080;">///</span> <span style="color: #808080;"><param name="enumDisplayText"></span><span style="color: #008000;">描述内容</span><span style="color: #808080;"></param></span></br> <span style="color: #808080;">///</span> <span style="color: #808080;"><param name="enumRank"></span><span style="color: #008000;">排列顺序</span><span style="color: #808080;"></param></span></br> <span style="color: #0000ff;">public</span> EnumDescription( <span style="color: #0000ff;">string</span> enumDisplayText, <span style="color: #0000ff;">int</span><span style="color: #000000;"> enumRank )</br> {</br> </span><span style="color: #0000ff;">this</span>.enumDisplayText =<span style="color: #000000;"> enumDisplayText;</br> </span><span style="color: #0000ff;">this</span>.enumRank =<span style="color: #000000;"> enumRank;</br> }</br></br> </span><span style="color: #808080;">///</span> <span style="color: #808080;"><summary></span></br> <span style="color: #808080;">///</span><span style="color: #008000;"> 描述枚举值,默认排序为5</br> </span><span style="color: #808080;">///</span> <span style="color: #808080;"></summary></span></br> <span style="color: #808080;">///</span> <span style="color: #808080;"><param name="enumDisplayText"></span><span style="color: #008000;">描述内容</span><span style="color: #808080;"></param></span></br> <span style="color: #0000ff;">public</span> EnumDescription( <span style="color: #0000ff;">string</span><span style="color: #000000;"> enumDisplayText )</br> : </span><span style="color: #0000ff;">this</span>(enumDisplayText, <span style="color: #800080;">5</span><span style="color: #000000;">) { }</br></br> </span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">string</span><span style="color: #000000;"> EnumDisplayText</br> {</br> </span><span style="color: #0000ff;">get</span> { <span style="color: #0000ff;">return</span> <span style="color: #0000ff;">this</span><span style="color: #000000;">.enumDisplayText; }</br> }</br> </span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">int</span><span style="color: #000000;"> EnumRank</br> {</br> </span><span style="color: #0000ff;">get</span> { <span style="color: #0000ff;">return</span><span style="color: #000000;"> enumRank; }</br> }</br> </span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">int</span><span style="color: #000000;"> EnumValue</br> {</br> </span><span style="color: #0000ff;">get</span> { <span style="color: #0000ff;">return</span> (<span style="color: #0000ff;">int</span>)fieldIno.GetValue(<span style="color: #0000ff;">null</span><span style="color: #000000;">); }</br> }</br> </span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">string</span><span style="color: #000000;"> FieldName</br> {</br> </span><span style="color: #0000ff;">get</span> { <span style="color: #0000ff;">return</span><span style="color: #000000;"> fieldIno.Name; }</br> }</br> </span><span style="color: #0000ff;">#region</span> =========================================对枚举描述属性的解释相关函数</br> <span style="color: #808080;">///</span> <span style="color: #808080;"><summary></span></br> <span style="color: #808080;">///</span><span style="color: #008000;"> 排序类型</br> </span><span style="color: #808080;">///</span> <span style="color: #808080;"></summary></span></br> <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">enum</span><span style="color: #000000;"> SortType</br> { </span><span style="color: #808080;">///</span> <span style="color: #808080;"><summary></span></br> <span style="color: #808080;">///</span><span style="color: #008000;">按枚举顺序默认排序</br> </span><span style="color: #808080;">///</span> <span style="color: #808080;"></summary></span></br>
Default,
/// <summary>
/// 按描述值排序
/// </summary>
DisplayText,
/// <summary>
/// 按排序熵
/// </summary>
Rank
}</span><span style="color: #0000ff;">private</span> <span style="color: #0000ff;">static</span> System.Collections.Hashtable cachedEnum = <span style="color: #0000ff;">new</span><span style="color: #000000;"> Hashtable();</br> </span><span style="color: #808080;">///</span> <span style="color: #808080;"><summary></span></br> <span style="color: #808080;">///</span><span style="color: #008000;"> 得到对枚举的描述文本</br> </span><span style="color: #808080;">///</span> <span style="color: #808080;"></summary></span></br> <span style="color: #808080;">///</span> <span style="color: #808080;"><param name="enumType"></span><span style="color: #008000;">枚举类型</span><span style="color: #808080;"></param></span></br> <span style="color: #808080;">///</span> <span style="color: #808080;"><returns></returns></span></br> <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">static</span> <span style="color: #0000ff;">string</span><span style="color: #000000;"> GetEnumText( Type enumType ) {</br> EnumDescription[] eds </span>= (EnumDescription[])enumType.GetCustomAttributes(<span style="color: #0000ff;">typeof</span>(EnumDescription), <span style="color: #0000ff;">false</span><span style="color: #000000;">);</br> </span><span style="color: #0000ff;">if</span> ( eds.Length != <span style="color: #800080;">1</span> ) <span style="color: #0000ff;">return</span> <span style="color: #0000ff;">string</span><span style="color: #000000;">.Empty;</br> </span><span style="color: #0000ff;">return</span> eds[<span style="color: #800080;">0</span><span style="color: #000000;">].EnumDisplayText;</br> }</br> </span><span style="color: #808080;">///</span> <span style="color: #808080;"><summary></span></br> <span style="color: #808080;">///</span><span style="color: #008000;"> 获得指定枚举类型中,指定值的描述文本。</br> </span><span style="color: #808080;">///</span> <span style="color: #808080;"></summary></span></br> <span style="color: #808080;">///</span> <span style="color: #808080;"><param name="enumValue"></span><span style="color: #008000;">枚举值,不要作任何类型转换</span><span style="color: #808080;"></param></span></br> <span style="color: #808080;">///</span> <span style="color: #808080;"><returns></span><span style="color: #008000;">描述字符串</span><span style="color: #808080;"></returns></span></br> <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">static</span> <span style="color: #0000ff;">string</span> GetFieldText( <span style="color: #0000ff;">object</span><span style="color: #000000;"> enumValue )</br> {</br> EnumDescription[] descriptions </span>=<span style="color: #000000;"> GetFieldTexts(enumValue.GetType(), SortType.Default);</br> </span><span style="color: #0000ff;">foreach</span> ( EnumDescription ed <span style="color: #0000ff;">in</span><span style="color: #000000;"> descriptions )</br> {</br> </span><span style="color: #0000ff;">if</span> ( ed.fieldIno.Name == enumValue.ToString() ) <span style="color: #0000ff;">return</span><span style="color: #000000;"> ed.EnumDisplayText; }</br> </span><span style="color: #0000ff;">return</span> <span style="color: #0000ff;">string</span><span style="color: #000000;">.Empty;</br> }</br></br> </span><span style="color: #808080;">///</span> <span style="color: #808080;"><summary></span></br> <span style="color: #808080;">///</span><span style="color: #008000;"> 得到枚举类型定义的所有文本,按定义的顺序返回</br> </span><span style="color: #808080;">///</span> <span style="color: #808080;"></summary></span></br> <span style="color: #808080;">///</span> <span style="color: #808080;"><exception cref="NotSupportedException"></exception></span></br> <span style="color: #808080;">///</span> <span style="color: #808080;"><param name="enumType"></span><span style="color: #008000;">枚举类型</span><span style="color: </br>#808080;"></param></span></br> <span style="color: #808080;">///</span> <span style="color: #808080;"><returns></span><span style="color: #008000;">所有定义的文本</span><span style="color: #808080;"></returns></span></br> <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">static</span><span style="color: #000000;"> EnumDescription[] GetFieldTexts( Type enumType )</br> {</br> </span><span style="color: #0000ff;">return</span><span style="color: #000000;"> GetFieldTexts(enumType, SortType.Default);</br> }</br> </span><span style="color: #808080;">///</span> <span style="color: #808080;"><summary></span></br> <span style="color: #808080;">///</span><span style="color: #008000;"> 得到枚举类型定义的所有文本</br> </span><span style="color: #808080;">///</span> <span style="color: #808080;"></summary></span></br> <span style="color: #808080;">///</span> <span style="color: #808080;"><exception cref="NotSupportedException"></exception></span></br> <span style="color: #808080;">///</span> <span style="color: #808080;"><param name="enumType"></span><span style="color: #008000;">枚举类型</span><span style="color: #808080;"></param></span></br> <span style="color: #808080;">///</span> <span style="color: #808080;"><param name="sortType"></span><span style="color: #008000;">指定排序类型</span><span style="color: #808080;"></param></span></br> <span style="color: #808080;">///</span> <span style="color: #808080;"><returns></span><span style="color: #008000;">所有定义的文本</span><span style="color: #808080;"></returns></span></br> <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">static</span><span style="color: #000000;"> EnumDescription[] GetFieldTexts( Type enumType, SortType sortType )</br> {</br> EnumDescription[] descriptions </span>= <span style="color: #0000ff;">null</span><span style="color: #000000;">;</br> </span><span style="color: #008000;">//</span><span style="color: #008000;">缓存中没有找到,通过反射获得字段的描述信息</span></br> <span style="color: #0000ff;">if</span> ( cachedEnum.Contains(enumType.FullName) == <span style="color: #0000ff;">false</span><span style="color: #000000;"> )</br> {</br> FieldInfo[] fields </span>=<span style="color: #000000;"> enumType.GetFields();</br> ArrayList edAL </span>= <span style="color: #0000ff;">new</span><span style="color: #000000;"> ArrayList();</br> </span><span style="color: #0000ff;">foreach</span> ( FieldInfo fi <span style="color: #0000ff;">in</span><span style="color: #000000;"> fields )</br> {</br> </span><span style="color: #0000ff;">object</span>[] eds = fi.GetCustomAttributes(<span style="color: #0000ff;">typeof</span>(EnumDescription), <span style="color: #0000ff;">false</span><span style="color: #000000;">);</br> </span><span style="color: #0000ff;">if</span> ( eds.Length != <span style="color: #800080;">1</span> ) <span style="color: #0000ff;">continue</span><span style="color: #000000;">;</br> ((EnumDescription)eds[</span><span style="color: #800080;">0</span>]).fieldIno =<span style="color: #000000;"> fi;</br> edAL.Add(eds[</span><span style="color: #800080;">0</span><span style="color: #000000;">]);</br> }</br> cachedEnum.Add(enumType.FullName, (EnumDescription[])edAL.ToArray(</span><span style="color: #0000ff;">typeof</span><span style="color: #000000;">(EnumDescription)));</br> }</br> descriptions </span>=<span style="color: #000000;"> (EnumDescription[])cachedEnum[enumType.FullName];</br> </span><span style="color: #0000ff;">if</span> ( descriptions.Length <= <span style="color: #800080;">0</span> ) <span style="color: #0000ff;">throw</span> <span style="color: #0000ff;">new</span> NotSupportedException(<span style="color: #800000;">"</span><span style="color: #800000;">枚举类型[</span><span style="color: #800000;">"</span> + enumType.Name + <span style="color: #800000;">"</span><span style="color: #800000;">]未定义属性EnumValueDescription</span><span style="color: #800000;">"</span><span style="color: #000000;">);</br> </span><span style="color: #008000;">//</span><span style="color: #008000;">按指定的属性冒泡排序</span></br> <span style="color: #0000ff;">for</span> ( <span style="color: #0000ff;">int</span> m = <span style="color: #800080;">0</span>; m < descriptions.Length; m++<span style="color: #000000;"> ) {</br> </span><span style="color: #008000;">//</span><span style="color: #008000;">默认就不排序了</span></br> <span style="color: #0000ff;">if</span> ( sortType == SortType.Default ) <span style="color: #0000ff;">break</span><span style="color: #000000;">;</br> </span><span style="color: #0000ff;">for</span> ( <span style="color: #0000ff;">int</span> n = m; n < descriptions.Length; n++<span style="color: #000000;"> )</br> {</br> EnumDescription temp;</br> </span><span style="color: #0000ff;">bool</span> swap = <span style="color: #0000ff;">false</span><span style="color: #000000;">;</br> </span><span style="color: #0000ff;">switch</span><span style="color: #000000;"> ( sortType )</br> { </span><span style="color: #0000ff;">case</span><span style="color: #000000;"> SortType.Default:</br> </span><span style="color: #0000ff;">break</span><span style="color: #000000;">;</br> </span><span style="color: #0000ff;">case</span><span style="color: #000000;"> SortType.DisplayText:</br> </span><span style="color: #0000ff;">if</span> ( <span style="color: #0000ff;">string</span>.Compare(descriptions[m].EnumDisplayText, descriptions[n].EnumDisplayText) > <span style="color: #800080;">0</span> ) swap = <span style="color: #0000ff;">true</span><span style="color: #000000;">;</br> </span><span style="color: #0000ff;">break</span><span style="color: #000000;">;</br> </span><span style="color: #0000ff;">case</span><span style="color: #000000;"> SortType.Rank:</br> </span><span style="color: #0000ff;">if</span> ( descriptions[m].EnumRank > descriptions[n].EnumRank ) swap = <span style="color: #0000ff;">true</span><span style="color: #000000;">;</br> </span><span style="color: #0000ff;">break</span><span style="color: #000000;">;</br> }</br> </span><span style="color: #0000ff;">if</span><span style="color: #000000;"> ( swap )</br> {</br> temp </span>=<span style="color: #000000;"> descriptions[m];</br> descriptions[m] </span>=<span style="color: #000000;"> descriptions[n];</br> descriptions[n] </span>=<span style="color: #000000;"> temp;</br> }</br> }</br> }</br> </span><span style="color: #0000ff;">return</span><span style="color: #000000;"> descriptions;</br> } </span><span style="color: #0000ff;">#endregion</span><span style="color: #000000;"></br> }</span></pre>