枚举的变换

枚举用于选择下拉框

 1 /// 
 2         /// 将枚举绑定到下拉框
 3         /// 
 4         /// 
 5         /// 选中值
 6         /// 提示语
 7         /// 是否显示提示语
 8         /// 排除不显示的枚举值(索引)
 9         /// 
10         public static SelectList EnumToListItem(int? p_Value, string p_Tip = "", bool IsShowSelectTip = true, int RemoveIndex = -10)
11         {
12             object _selectValue = null;
13             if (p_Value.HasValue)
14             {
15                 _selectValue = p_Value.Value;
16             }
17             var item = new List();
18             if (IsShowSelectTip)
19             {
20                 item.Add(new SelectListItem { Value = "", Text = "--请选择" + p_Tip + "--" });
21             }
22             foreach (int s in Enum.GetValues(typeof(T)))
23             {
24                 //item.Add(new SelectListItem() { Text = Enum.GetName(typeof(T), s), Value = s.ToString() });
25                 if (RemoveIndex != s)
26                 {
27                     item.Add(new SelectListItem { Text = Enum.GetName(typeof(T), s), Value = s.ToString() });
28                 }
29             }
30             var list = new SelectList(item, "Value", "Text", _selectValue);
31             return list;
32         }
View Code

将枚举转换为字典

 1 /// 
 2         /// 将枚举转换成字典集合
 3         /// 
 4         public static IDictionary<object, object> EnumToList()
 5         {
 6             Dictionary<object, object> m_Dic = new Dictionary<object, object>();
 7             string m_Value = string.Empty;
 8             foreach (int s in Enum.GetValues(typeof(T)))
 9             {
10                 m_Value = Enum.GetName(typeof(T), s);
11                 m_Dic.Add(s, m_Value);
12             }
13             return m_Dic;
14         }
View Code

 

转载于:https://www.cnblogs.com/HuberyHu/p/5319253.html

你可能感兴趣的:(枚举的变换)