C#枚举高级应用
2011-10-14 11:53
302 查看
我们经常使用枚举,但是可能只是一些简单的应用,其实在我们的项目中用到的 下拉列表、Model属性类型等,都可以利用枚举更好的实现。 //定义枚举 public enum DepartmentEum { [Description("第一产品事业部")] FirstDept = 1 } 1、下拉列表使用枚举数据源: //绑定部门 var deptList = EnumHelper.GetCachedDictionary(typeof(DepartmentEum )); var ddlDeptList = new List<SelectListItem>(); ddlDeptList.Add(new SelectListItem() { Text = "请选择", Value = "-1" }); foreach (var item in deptList) { ddlDeptList.Add(new SelectListItem() { Text = item.Value, Value = item.Key }); } 2、Model属性类型: /// <summary> /// 部门枚举 /// </summary> [DataMember] public DepartmentEum Department { get; set; } //获取枚举值 (int)model.Department //获取枚举描述信息 EnumHelper.GetEnumDescription(model.Department) /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// 3、以下是枚举帮助类 EnumHelper.cs public static class EnumHelper { private static readonly Hashtable ht = Hashtable.Synchronized(new Hashtable()); /// <summary> /// 从缓存中获取枚举对应的List /// </summary> /// <param name="type"></param> /// <returns></returns> public static IList<KeyValuePair<string, string>> GetCachedDictionary(Type type) { if (ht.Contains(type)) { return ht[type] as IList<KeyValuePair<string, string>>; } else { IList<KeyValuePair<string, string>> dict = GetDictionary(type); ht[type] = dict; return dict; } } public static IList<KeyValuePair<string, string>> GetDictionary(Type type) { if (!type.IsEnum) { throw new InvalidOperationException("错误的枚举类型"); } FieldInfo[] fields = type.GetFields(); IList<KeyValuePair<string, string>> dict = new List<KeyValuePair<string, string>>(); foreach (var item in fields) { if (item.FieldType.IsEnum == false) { continue; } string desription = string.Empty; object[] objs = item.GetCustomAttributes(typeof (DescriptionAttribute), false); if (objs != null && objs.Length> 0) { DescriptionAttribute da = (DescriptionAttribute) objs[0]; desription = da.Description; } else { desription = item.Name; } dict.Add(new KeyValuePair<string, string>(((int) Enum.Parse(type, item.Name)).ToString(), desription)); } return dict; } public static string GetCachedDescription(Type type, string fieldName) { IList<KeyValuePair<string, string>> dictionary = GetCachedDictionary(type); foreach (KeyValuePair<string, string> keyValuePair in dictionary) { if (keyValuePair.Key == ((int) (Enum.Parse(type, fieldName))).ToString()) { return keyValuePair.Value; } } return fieldName; } public static string GetCachedFieldName(Type type, string description) { IList<KeyValuePair<string, string>> dictionary = GetCachedDictionary(type); foreach (KeyValuePair<string, string> keyValuePair in dictionary) { if(keyValuePair.Value == description) { return keyValuePair.Key; } } return description; } ///<summary> /// 根据枚举类型获取描述 ///</summary> ///<param name="enumSubitem">类型</param> ///<returns>描述</returns> public static string GetEnumDescription(Enum value) { // Get the Description attribute value for the enum value FieldInfo fi = value.GetType().GetField(value.ToString()); DescriptionAttribute[] attributes = (DescriptionAttribute[])fi.GetCustomAttributes(typeof(DescriptionAttribute), false); if (attributes.Length > 0) { return attributes[0].Description; } else { return value.ToString(); } } /// <summary> /// 从缓存中获取枚举对应的List[添加一空白列] /// </summary> /// <param name="type"></param> /// <returns></returns> public static IList<KeyValuePair<string, string>> GetCachedDictionaryBlank(Type type) { if (ht.Contains(type)) { return ht[type] as IList<KeyValuePair<string, string>>; } IList<KeyValuePair<string, string>> dict = GetDictionary(type); dict.Insert(0, new KeyValuePair<string, string>((-1).ToString(), string.Empty)); ht[type] = dict; return dict; } public static KeyValuePair<string, string> GetEnum(Type type, string fieldName) { KeyValuePair<string, string> kvp = new KeyValuePair<string, string>(); IList<KeyValuePair<string, string>> dictionary = GetCachedDictionary(type); foreach (KeyValuePair<string, string> keyValuePair in dictionary) { if (keyValuePair.Key == ((int)(Enum.Parse(type, fieldName))).ToString()) { kvp = keyValuePair; break; } } return kvp; } }
相关文章推荐
- C#中正则表达式的高级应用
- C#枚举的应用
- c#事务、using释放资源、实体类的高级应用
- c#枚举类型在web控件CheckBoxList,RadioButtonList,DropDownList中的应用
- C# 委托高级应用----线程——创建无阻塞的异步调用(一)
- C# 枚举在项目中的应用总结
- C#中正则表达式的高级应用
- C# 枚举特性 FlagAttribute 的应用
- C# 委托高级应用----线程——创建无阻塞的异步调用(二)
- C#图片裁剪高级应用
- C#应用WindowsApi实现查找\枚举(FindWindow、EnumChildWindows)窗体控件,并发送消息。
- C#枚举,继承的应用-模拟动物的,青蛙,狗都继承动物
- c# 扩展方法奇思妙用高级篇三:Enumerable.Cast<T> 应用
- C# WebBrowser高级应用
- 黑马程序员_简单类实现枚举_枚举的高级应用
- 黑马程序员-jdk1.5新特性--枚举的高级应用。
- C#开发 —— 高级应用
- C#枚举的应用
- C#正则表达式(RegEx)高级应用之分组(Group)替换(Replace)
- C#图片处理高级应用(裁剪,缩放,清晰度,水印)