枚举定义如下:
代码
/// <summary>
/// 用户身份
/// </summary>
public enum EnumMemberStata
{
/// <summary>
/// 学生
/// </summary>
Student = 1,
/// <summary>
/// 家长
/// </summary>
Parents = 2,
/// <summary>
/// 教师
/// </summary>
Teacher = 3
}
/// 用户身份
/// </summary>
public enum EnumMemberStata
{
/// <summary>
/// 学生
/// </summary>
Student = 1,
/// <summary>
/// 家长
/// </summary>
Parents = 2,
/// <summary>
/// 教师
/// </summary>
Teacher = 3
}
前台使用还必须调用个方法,如下:
代码
public static string MemberStata(EnumMemberStata a)
{
switch (a)
{
case EnumMemberStata.Student:
return "学生";
case EnumMemberStata.Parents:
return "家长";
case EnumMemberStata.Teacher:
return "家长";
default:
return "学生";
}
}
{
switch (a)
{
case EnumMemberStata.Student:
return "学生";
case EnumMemberStata.Parents:
return "家长";
case EnumMemberStata.Teacher:
return "家长";
default:
return "学生";
}
}
我们可以通过为每个枚举值添加Description来简化。
修改枚举定义如下:
代码
/// <summary>
/// 用户身份
/// </summary>
public enum EnumMemberStata
{
/// <summary>
/// 学生
/// </summary>
[Description("学生")]
Student=1,
/// <summary>
/// 家长
/// </summary>
[Description("家长")]
Parents=2,
/// <summary>
/// 教师
/// </summary>
[Description("教师")]
Teacher=3
}
/// 用户身份
/// </summary>
public enum EnumMemberStata
{
/// <summary>
/// 学生
/// </summary>
[Description("学生")]
Student=1,
/// <summary>
/// 家长
/// </summary>
[Description("家长")]
Parents=2,
/// <summary>
/// 教师
/// </summary>
[Description("教师")]
Teacher=3
}
再写个方法来获取Description值,如下:
代码
using System.Reflection;
using System.ComponentModel;
using System.Collections.Specialized;
/// <summary>
/// 获取enumdesc
/// </summary>
/// <param name="value"></param>
/// <returns></returns>
public static string GetDescription(Enum value)
{
FieldInfo field = value.GetType().GetField(value.ToString());
DescriptionAttribute[] attributes = (DescriptionAttribute[])field.GetCustomAttributes(typeof(DescriptionAttribute), false);
return (attributes.Length > 0) ? attributes[0].Description : value.ToString();
}
using System.ComponentModel;
using System.Collections.Specialized;
/// <summary>
/// 获取enumdesc
/// </summary>
/// <param name="value"></param>
/// <returns></returns>
public static string GetDescription(Enum value)
{
FieldInfo field = value.GetType().GetField(value.ToString());
DescriptionAttribute[] attributes = (DescriptionAttribute[])field.GetCustomAttributes(typeof(DescriptionAttribute), false);
return (attributes.Length > 0) ? attributes[0].Description : value.ToString();
}
如果这个时候您觉得只有一个Description不够用,想再多添加自定义的属性.如:
代码
public class EnumDescriptionAttribute : Attribute
{
private string _text = "";
/// <summary>
/// Text describing the enum value
/// </summary>
public string Text
{
get { return this._text; }
}
/// <summary>
/// Instantiates the EnumDescriptionAttribute object
/// </summary>
/// <param name=”text”>Description of the enum value</param>
public EnumDescriptionAttribute(string text)
{
_text = text;
}
}
public enum MyEnum
{
[Description("男孩")]
[EnumDescriptionAttribute("男孩备注")]
Girl = 0,
[Description("女孩")]
[EnumDescriptionAttribute("女孩备注")]
Boy = 1
}
{
private string _text = "";
/// <summary>
/// Text describing the enum value
/// </summary>
public string Text
{
get { return this._text; }
}
/// <summary>
/// Instantiates the EnumDescriptionAttribute object
/// </summary>
/// <param name=”text”>Description of the enum value</param>
public EnumDescriptionAttribute(string text)
{
_text = text;
}
}
public enum MyEnum
{
[Description("男孩")]
[EnumDescriptionAttribute("男孩备注")]
Girl = 0,
[Description("女孩")]
[EnumDescriptionAttribute("女孩备注")]
Boy = 1
}
获取其属性值的代码如下:
代码
public static String GetEnumDesc(Enum e)
{
FieldInfo EnumInfo = e.GetType().GetField(e.ToString());
EnumDescriptionAttribute[] EnumAttributes = (EnumDescriptionAttribute[])EnumInfo.
GetCustomAttributes(typeof(EnumDescriptionAttribute), false);
if (EnumAttributes.Length > 0)
{
return EnumAttributes[0].Text;
}
return e.ToString();
}
{
FieldInfo EnumInfo = e.GetType().GetField(e.ToString());
EnumDescriptionAttribute[] EnumAttributes = (EnumDescriptionAttribute[])EnumInfo.
GetCustomAttributes(typeof(EnumDescriptionAttribute), false);
if (EnumAttributes.Length > 0)
{
return EnumAttributes[0].Text;
}
return e.ToString();
}
调用方法为:
EnumText.MyEnum aa = EnumText.MyEnum.Doctor;
Response.Write(GetVal.GetEnumDesc(aa));
当您做后台用户权限的时候如果用到枚举,这个时候您可能需要使用按位枚举:
[Flags]
public enum Permission
{
Select = 1,
Edit = 2,
Delete = 4,
View = 8,
All = Select | Edit | Delete | View
}
public enum Permission
{
Select = 1,
Edit = 2,
Delete = 4,
View = 8,
All = Select | Edit | Delete | View
}
参考:1.给枚举添加描述信息
2.用反射+特性列出所有的枚举变量及其描述信息,绑定到DropDownList上。
4.Text description for enum values
5. 关于枚举的种种 (Enumeration FAQ) [C#, IL, BCL]