C#+Enum 枚举扩展方法获取name,value和Description

 public enum ConvertDeliveryCode
        {
         [System.ComponentModel.Description("9980")] 犬山 = 9980,
            [System.ComponentModel.Description("9981")] 关原 = 9981,
            [System.ComponentModel.Description("9982")] 神户 = 9982,
            [System.ComponentModel.Description("9990")] 桶川 = 9990,
            [System.ComponentModel.Description("9991")] 秦野 = 9991,
            [System.ComponentModel.Description("9993")] 东松山 = 9993,
            [System.ComponentModel.Description("9994")] 北九州 = 9994,
            [System.ComponentModel.Description("9996")] 岗山 = 9996,
            [System.ComponentModel.Description("9998")] 名取 = 9998 

        }

第一种方法:

 [AttributeUsage(AttributeTargets.Field, AllowMultiple = false)]
        public sealed class ConvertEnumDescriptionAttribute : Attribute
        {
            private string description;
            public string Description
            {
                get { return this.description; }
            }

            public ConvertEnumDescriptionAttribute(string description)
                : base()
            {
                this.description = description;
            }
        }
        public static string GetDescription(Enum value)
        {
            if (value == null)
            {
                throw new ArgumentNullException("value");
            }
            string description = value.ToString();
            System.Reflection.FieldInfo fieldinfo = value.GetType().GetField(description);
            ConvertEnumDescriptionAttribute[] attributes = (ConvertEnumDescriptionAttribute[])fieldinfo.GetCustomAttributes(typeof(ConvertEnumDescriptionAttribute), false);
            if (attributes != null && attributes.Length > 0)
            {
                description = attributes[0].Description;
            }
            return description;
        }

第二种方法:
/// <summary>
        /// 扩展方法,获得枚举的Description
        /// </summary>
        /// <param name="value">枚举值</param>
        /// <param name="nameInstend">当枚举没有定义DescriptionAttribute,是否用枚举名代替,默认使用</param>
        /// <returns>枚举的Description</returns>
        public static string GetDescription(this Enum value, bool nameInstend = true)
        {
            Type type = value.GetType();
            string name = Enum.GetName(type, value);
            if (name==null)
            {
                return null;
            }            
System.Reflection.FieldInfo field = type.GetField(name);
            
System.ComponentModel.
DescriptionAttribute attribute = Attribute.GetCustomAttribute(field, typeof(
System.ComponentModel.
DescriptionAttribute)) as
System.ComponentModel.
DescriptionAttribute;
            if (attribute==null&&nameInstend==true)
            {
                return name;
            }
            return attribute==null? null :attribute.Description;
        }
 

使用方法:

第一种方法/第二种方法:

GetDescription(ConvertDeliveryCode.犬山)

result:9980
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值