• 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>
     
  • 相关阅读:
    Python十大经典算法之选择排序
    在js中修改样式带 !important 的样式
    vscode 使用 ejs 语法有红色错误提示线
    mysql 报 'Host ‘XXXXXX’ is blocked because of many connection errors'
    字符编码
    Canal.adapter报错
    Windows 域控配置时间同步
    Docker清理日志脚本
    Docker快速部署clickhouse
    Windows批处理一键添加hosts文件
  • 原文地址:https://www.cnblogs.com/zwei1121/p/5198508.html
Copyright © 2020-2023  润新知