【美高梅开户网址】枚举描述获取

 public static class EnumExtension
    {
        public static string GetDescription(this Enum value)
        {
            FieldInfo field =
value.GetType().GetField(value.ToString());
            DescriptionAttribute attribute =
Attribute.GetCustomAttribute(field, typeof(DescriptionAttribute)) as
DescriptionAttribute;

 public static class EnumExtension
    {
        public static string GetDescription(this Enum value)
        {
            FieldInfo field =
value.GetType().GetField(value.ToString());
            DescriptionAttribute attribute =
Attribute.GetCustomAttribute(field, typeof(DescriptionAttribute)) as
DescriptionAttribute;

C#枚举描述获取,

 public static class EnumExtension
    {
        public static string GetDescription(this Enum value)
        {
            FieldInfo field =
value.GetType().GetField(value.ToString());
            DescriptionAttribute attribute =
Attribute.GetCustomAttribute(field, typeof(DescriptionAttribute)) as
DescriptionAttribute;

            return attribute == null ? value.ToString() :
attribute.Description;
        }

        //public static IEnumerable<EnumItem>
GetDictionary<TEnum>(this TEnum tEnum)
        //    where TEnum : struct
        //{
【美高梅开户网址】枚举描述获取。        //    return
Enum.GetValues(typeof(TEnum)).OfType<Enum>()
        //        .Select(x => new EnumItem
        //        {
        //            Key = Convert.ToInt32(x),
        //            Value = x.ToString(),
        //            Description = x.GetDescription()
        //        });
        //}

        public static IEnumerable<EnumItem> GetDictionary(Type
tEnum)
        {
            return Enum.GetValues(tEnum).OfType<Enum>()
                .Select(x => new EnumItem
                {
                    Key = Convert.ToInt32(x),
                    Value = x.ToString(),
                    Description = x.GetDescription()
                });
        }
    }

public static class
EnumExtension { public static string GetDescription(this Enum value) {
FieldInfo field = value.GetType().GetField(value.ToString());
Des…

给枚举添加本性,通过反射获取个性字符,有个别需求把枚举呈现在界面选用的地点有效

            return attribute == null ? value.ToString() :
attribute.Description;
        }

            return attribute == null ? value.ToString() :
attribute.Description;
        }

 public static  class EnumDescription
    {
        /// <summary>
        /// 获取枚举的Description
        /// </summary>
        /// <param name=”value”>枚举值</param>
        /// <param
name=”nameInstead”>当枚举未有定义description时,是不是使用枚举名代替,默许使用</param>
        /// <returns>枚举的description</returns>
        public static string GetDescription(this Enum value, Boolean
nameInstead = true)
        {
            Type type = value.GetType();
            string name = Enum.GetName(type, value);
            if (name == null)
            {
                return null;
            }
            FieldInfo field = type.GetField(name);
            DescriptionAttribute attribute =
Attribute.GetCustomAttribute(field, typeof(DescriptionAttribute))
                as DescriptionAttribute;
            if (attribute == null && nameInstead == true)
            {
                return name;
            }
            return attribute == null ? null : attribute.Description;

        //public static IEnumerable<EnumItem>
GetDictionary<TEnum>(this TEnum tEnum)
        //    where TEnum : struct
        //{
        //    return
Enum.GetValues(typeof(TEnum)).OfType<Enum>()
        //        .Select(x => new EnumItem
        //        {
        //            Key = Convert.ToInt32(x),
        //            Value = x.ToString(),
        //            Description = x.GetDescription()
        //        });
        //}

        //public static IEnumerable<EnumItem>
GetDictionary<TEnum>(this TEnum tEnum)
        //    where TEnum : struct
        //{
        //    return
Enum.GetValues(typeof(TEnum)).OfType<Enum>()
        //        .Select(x => new EnumItem
        //        {
        //            Key = Convert.ToInt32(x),
        //            Value = x.ToString(),
        //            Description = x.GetDescription()
        //        });
        //}

        }
        /// <summary>
        /// 吧枚举转换为键值对聚集
        /// </summary>
        /// <param name=”enumType”>枚举类型</param>
        /// <param name=”getText”>可能值文本</param>
美高梅开户网址 ,        ///
<returns>以枚举值为key,枚举文本为value的键值对集合</returns>
        public static Dictionary<Int32, String>
EnumToDescription(Type enumType, Func<Enum, String> getText)
        {
            if (!enumType.IsEnum)
            {
                throw new
ArgumentException(“传入的参数必需是枚举类型!”, “enumType”);

        public static IEnumerable<EnumItem> GetDictionary(Type
tEnum)
        {
            return Enum.GetValues(tEnum).OfType<Enum>()
                .Select(x => new EnumItem
                {
                    Key = Convert.ToInt32(x),
                    Value = x.ToString(),
                    Description = x.GetDescription()
                });
        }
    }

        public static IEnumerable<EnumItem> GetDictionary(Type
tEnum)
        {
            return Enum.GetValues(tEnum).OfType<Enum>()
                .Select(x => new EnumItem
                {
                    Key = Convert.ToInt32(x),
                    Value = x.ToString(),
                    Description = x.GetDescription()
                });
        }
    }

            }
            Dictionary<int ,string>enumDic=new
Dictionary<int,string>();
            Array enumValues=Enum.GetValues(enumType);
            foreach(Enum enumValue in enumValues)
            {
                Int32 key=Convert.ToInt32(enumValue);
                String value=getText(enumValue);
                enumDic.Add(key,value);
            }
            return enumDic;

        }
    }

 

首先个法子为枚举扩充方法,借使枚举未有Description 暗中认可输出为枚举名,不然输出空

第3个章程经枚举转换为以枚举值为key,以Description为value的键对集合

实例

     public enum Person
    {
        [Description(“张三”)]
        zhangsan=1,
        [Description(“李四”)]
        lisi=2,
        wangwu
    }

 class Program

{

 static void Main(string[] args)

{

            Person p = Person.wangwu;
            Console.WriteLine(p.GetDescription(false));
            Console.WriteLine(p.GetDescription());
            p = Person.zhangsan;
            Console.WriteLine(p.GetDescription());
            Dictionary<Int32, String> dic =
EnumDescription.EnumToDescription(typeof(Person),
                e => e.GetDescription());
           
            printdic(dic);

}

      static void printdic(Dictionary<Int32, String> dic)
        {
            foreach (KeyValuePair<Int32, String> item in dic)
            {
                Console.WriteLine(“{0}–{1}”, item.Key, item.Value);
            }
        }

}

结果:

 美高梅开户网址 1

 

Disciption  供给引用using System.ComponentModel;

FieldInfo  要求引用using System.Reflection;

 

关于扩张方法:

扩大方法使你能够向现有项目“添加”方法,而无需创造新的派生类型、重新编写翻译或以其余措施修改原始类型。

必须是静态类,写法如首先个办法

=> 符号:

lamda表达式····

e=>e.GetDescription()

类似 

string getText(Person p)

{

return p.GetDescription();

}

 

发表评论

电子邮件地址不会被公开。 必填项已用*标注

网站地图xml地图