C# 獲取列舉的描述Description
阿新 • • 發佈:2018-11-02
方法類:
public static class EnumExtensions { #region Enum /// <summary> /// 獲取列舉變數值的 Description 屬性 /// </summary> /// <param name="obj">列舉變數</param> /// <returns>如果包含 Description 屬性,則返回 Description 屬性的值,否則返回列舉變數值的名稱</returns>public static string GetDescription(this System.Enum obj) { return GetDescription(obj, false); } /// <summary> /// 獲取列舉變數值的 Description 屬性 /// </summary> /// <param name="obj">列舉變數</param> /// <param name="isTop">是否改變為返回該類、列舉型別的頭 Description 屬性,而不是當前的屬性或列舉變數值的 Description 屬性</param> /// <returns>如果包含 Description 屬性,則返回 Description 屬性的值,否則返回列舉變數值的名稱</returns> public static string GetDescription(this System.Enum obj, bool isTop) { if (obj == null) {return string.Empty; } try { Type enumType = obj.GetType(); DescriptionAttribute dna = null; if (isTop) { dna = (DescriptionAttribute)Attribute.GetCustomAttribute(enumType, typeof(DescriptionAttribute)); } else { FieldInfo fi = enumType.GetField(System.Enum.GetName(enumType, obj)); dna = (DescriptionAttribute)Attribute.GetCustomAttribute( fi, typeof(DescriptionAttribute)); } if ((dna != null) && (string.IsNullOrEmpty(dna.Description) == false)) { return dna.Description; } } catch { } return obj.ToString(); } /// <summary> /// 獲取列舉變數值的 Description 屬性 /// </summary> /// <param name="obj">列舉變數</param> /// <returns>如果包含 Description 屬性,則返回 Description 屬性的值,否則返回列舉變數值的名稱</returns> public static string GetDescription<TEnum>(this TEnum obj) { return GetDescription(obj, false); } /// <summary> /// 獲取列舉變數值的 Description 屬性 /// </summary> /// <param name="obj">列舉變數</param> /// <param name="isTop">是否改變為返回該類、列舉型別的頭 Description 屬性,而不是當前的屬性或列舉變數值的 Description 屬性</param> /// <returns>如果包含 Description 屬性,則返回 Description 屬性的值,否則返回列舉變數值的名稱</returns> public static string GetDescription<TEnum>(this TEnum obj, bool isTop) { if (obj == null) { return string.Empty; } try { Type enumType = obj.GetType(); DescriptionAttribute dna = null; if (isTop) { dna = (DescriptionAttribute)Attribute.GetCustomAttribute(enumType, typeof(DescriptionAttribute)); } else { FieldInfo fi = enumType.GetField(System.Enum.GetName(enumType, obj)); dna = (DescriptionAttribute)Attribute.GetCustomAttribute( fi, typeof(DescriptionAttribute)); } if ((dna != null) && (string.IsNullOrEmpty(dna.Description) == false)) { return dna.Description; } } catch { } return obj.ToString(); } /// <summary> /// 獲取欄位Description /// </summary> /// <param name="fieldInfo">FieldInfo</param> /// <returns>DescriptionAttribute[] </returns> public static DescriptionAttribute[] GetDescriptAttr(this FieldInfo fieldInfo) { if (fieldInfo != null) { return (DescriptionAttribute[])fieldInfo.GetCustomAttributes(typeof(DescriptionAttribute), false); } return null; } /// <summary> /// 根據Description獲取列舉 /// 說明: /// 單元測試-->通過 /// </summary> /// <typeparam name="T">列舉型別</typeparam> /// <param name="description">列舉描述</param> /// <returns>列舉</returns> public static T GetEnumName<T>(string description) { Type _type = typeof(T); foreach (FieldInfo field in _type.GetFields()) { DescriptionAttribute[] _curDesc = field.GetDescriptAttr(); if (_curDesc != null && _curDesc.Length > 0) { if (_curDesc[0].Description == description) return (T)field.GetValue(null); } else { if (field.Name == description) return (T)field.GetValue(null); } } throw new ArgumentException(string.Format("{0} 未能找到對應的列舉.錯誤列舉{1}", description, _type.Name), "Description"); } /// <summary> /// 將列舉轉換為ArrayList /// 說明: /// 若不是列舉型別,則返回NULL /// 單元測試-->通過 /// </summary> /// <param name="type">列舉型別</param> /// <returns>ArrayList</returns> public static ArrayList ToArrayList(this Type type) { if (type.IsEnum) { ArrayList _array = new ArrayList(); Array _enumValues = System.Enum.GetValues(type); foreach (System.Enum value in _enumValues) { _array.Add(new KeyValuePair<System.Enum, string>(value, GetDescription(value))); } return _array; } return null; } #endregion Enum }
列舉類:
#region 職員流程型別 /// <summary> /// 職員流程型別 /// </summary> [Description("職員流程型別")] public enum EmployeeAffairTypeEnum { /// <summary> /// 入職 /// </summary> [Description("入職")] ApplyInureEmpInPositon, /// <summary> /// 轉正 /// </summary> [Description("轉正")] ApplyInureEmpInPass, /// <summary> /// 兼職 /// </summary> [Description("兼職")] ApplyInureEmpPartJob, /// <summary> /// 結束兼職 /// </summary> [Description("結束兼職")] ApplyInureEmpEndPartJob, /// <summary> /// 異動 /// </summary> [Description("異動")] ApplyInureEmpInAdjust, /// <summary> /// 離職 /// </summary> [Description("離職")] ApplyInureEmpInDimission, /// <summary> /// 正式合同續簽 /// </summary> [Description("正式合同續簽")] ApplyInureEmpContractRenewal, /// <summary> /// 職員群調審批 /// </summary> [Description("職員群調審批")] ApplyInureEmpGroupTransfer, /// <summary> /// 行車津貼 /// </summary> [Description("行車津貼")] ApplyInureEmpMileage } #endregion 職員流程型別 #region 職員申請單資料(入職、轉正、異動、離職、兼職、結束兼職、合同續簽、職員異動)的搜尋檢視 /// <summary> /// 職員申請單資料(入職、轉正、異動、離職、兼職、結束兼職、合同續簽、職員異動)的搜尋檢視 /// </summary> [Description("職員申請表檢視")] public enum EmployeeApplyViewEnum { /// <summary> /// 入職 /// </summary> [Description("VM_ApplyInureEmpInPositon")] ApplyInureEmpInPositon, /// <summary> /// 轉正 /// </summary> [Description("VM_ApplyInureEmpInPass")] ApplyInureEmpInPass, /// <summary> /// 兼職 /// </summary> [Description("VM_ApplyInureEmpPartJob")] ApplyInureEmpPartJob, /// <summary> /// 結束兼職 /// </summary> [Description("VM_ApplyInureEmpEndPartJob")] ApplyInureEmpEndPartJob, /// <summary> /// 異動 /// </summary> [Description("VM_ApplyInureEmpInAdjust")] ApplyInureEmpInAdjust, /// <summary> /// 離職 /// </summary> [Description("VM_ApplyInureEmpInDimission")] ApplyInureEmpInDimission, /// <summary> /// 正式合同續簽 /// </summary> [Description("VM_ApplyInureEmpContractRenewal")] ApplyInureEmpContractRenewal, /// <summary> /// 職員群調審批 /// </summary> [Description("VM_ApplyInureEmpGroupTransfer")] ApplyInureEmpGroupTransfer, /// <summary> /// 行車津貼 /// </summary> [Description("VM_ApplyInureEmpMileage")] ApplyInureEmpMileage } #endregion
呼叫方法:
var affairDescription = EmployeeAffairTypeEnum.ApplyInureEmpContractRenewal.GetDescription(); Console.WriteLine(affairDescription); var viewName = ((EmployeeApplyViewEnum)EmployeeAffairTypeEnum.ApplyInureEmpContractRenewal).GetDescription(); Console.WriteLine(viewName);