威尼斯人线上娱乐

枚举描述获取

6 4月 , 2019  

 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 私下认可输出为枚举名,不然输出空

其次个方法经枚举转换为以枚举值为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地图