zoukankan      html  css  js  c++  java
  • enum

    public static class EnumEx
    {
        public static T GetValueFromDescription<T>(string description)
        {
            var type = typeof(T);
            if(!type.IsEnum) throw new InvalidOperationException();
            foreach(var field in type.GetFields())
            {
                var attribute = Attribute.GetCustomAttribute(field,
                    typeof(DescriptionAttribute)) as DescriptionAttribute;
                if(attribute != null)
                {
                    if(attribute.Description == description)
                        return (T)field.GetValue(null);
                }
                else
                {
                    if(field.Name == description)
                        return (T)field.GetValue(null);
                }
            }
            throw new ArgumentException("Not found.", "description");
            // or return default(T);
        }
    }
    

    Usage:

    var panda = EnumEx.GetValueFromDescription<Animal>("Giant Panda");





    public static class EnumHelper<T>
    {
        public static IList<T> GetValues(Enum value)
        {
            var enumValues = new List<T>();
    
            foreach (FieldInfo fi in value.GetType().GetFields(BindingFlags.Static | BindingFlags.Public))
            {
                enumValues.Add((T)Enum.Parse(value.GetType(), fi.Name, false));
            }
            return enumValues;
        }
    
        public static T Parse(string value)
        {
            return (T)Enum.Parse(typeof(T), value, true);
        }
    
        public static IList<string> GetNames(Enum value)
        {
            return value.GetType().GetFields(BindingFlags.Static | BindingFlags.Public).Select(fi => fi.Name).ToList();
        }
    
        public static IList<string> GetDisplayValues(Enum value)
        {
            return GetNames(value).Select(obj => GetDisplayValue(Parse(obj))).ToList();
        }
    
        public static string GetDisplayValue(T value)
        {
            var fieldInfo = value.GetType().GetField(value.ToString());
    
            var descriptionAttributes = fieldInfo.GetCustomAttributes(
                typeof(DisplayAttribute), false) as DisplayAttribute[];
    
            if (descriptionAttributes == null) return string.Empty;
            return (descriptionAttributes.Length > 0) ? descriptionAttributes[0].Name : value.ToString();
        }
    }


    <ul>
        @foreach (var value in @EnumHelper<UserPromotion>.GetValues(UserPromotion.None))
        {
             if (value == Model.JobSeeker.Promotion)
            {
                var description = EnumHelper<UserPromotion>.GetDisplayValue(value);
                <li>@Html.DisplayFor(e => description )</li>
            }
        }
    </ul>
     
  • 相关阅读:
    LintCode 9.Fizz Buzz 问题(JAVA实现,一个if都不用)
    Docker中使用ElasticSearch
    Docker中使用RabbitMQ
    SpringBoot 缓存工作原理
    SpringBoot 启动配置原理
    docker 常用命令
    SpringBoot 自动配置原理
    动态规划求斐波那契数列
    MySQL 日期加减
    【Linux】Ubuntu:取消用户登录密码
  • 原文地址:https://www.cnblogs.com/zwei1121/p/5198508.html
Copyright © 2011-2022 走看看