C# 获取枚举的描述Description

方法类:

    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);

 

转载于:https://www.cnblogs.com/gygtech/p/9894439.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值