c# 获取枚举描述的扩展方法

/// 
    /// 枚举扩展类
    /// 
    public static class EnumExtension
    {
        /// 
        /// 获取枚举的描述信息
        /// 
        public static string GetDescription(this Enum em)
        {
            Type type = em.GetType();
            FieldInfo fd = type.GetField(em.ToString());
            if (fd == null)
                return string.Empty;
            object[] attrs = fd.GetCustomAttributes(typeof(DescriptionAttribute), false);
            string name = string.Empty;
            foreach (DescriptionAttribute attr in attrs)
            {
                name = attr.Description;
            }
            return name;
        }

        /// 
        /// 根据枚举值的list 返回所有的描述
        /// 
        /// 
        /// 
        /// 
        public static string Descriptionstr(this List list)
        {
            var des = string.Empty;
            foreach (var value in list)
            {
                des = des + ((Enum)Enum.Parse(typeof(T), value)).GetDescription() + ",";
            }
            return des.TrimEnd(',');
        }

        /// 
        /// 根据枚举值的list 返回所有的描述
        /// 
        /// 
        /// 
        /// 
        public static string Descriptionstr(this List list)
        {
            var des = string.Empty;
            foreach (var value in list)
            {
                des = des + ((Enum)Enum.ToObject(typeof(T), value)).GetDescription() + ",";
            }
            return des.TrimEnd(',');
        }
    }

你可能感兴趣的:(C#,c#,linq,java)