#region 枚举属性扩展类 /// <summary> /// 枚举扩展方法 /// </summary> public static class EnumExtensions { public static string GetDescription(this Enum obj) { return GetDescription(obj, false); } public static string GetDescription(this 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(Enum.GetName(_enumType, obj)); dna = (DescriptionAttribute)Attribute.GetCustomAttribute( fi, typeof(DescriptionAttribute)); } if (dna != null && string.IsNullOrEmpty(dna.Description) == false) return dna.Description; } catch { throw; } return obj.ToString(); } } #endregion
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)