我們經常使用枚舉,但是可能只是一些簡單的應用,其實在我們的項目中用到的 下拉列表、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;
}
}