zoukankan      html  css  js  c++  java
  • C# Enum扩展

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Reflection;
    
    namespace Automatic_Mouse_Click
    {
        internal static class EnumExt
        {
            /// <summary>
            /// 枚举转换为列表;Id表示当前序号;value表示常量值
            /// 支持自定义显示名称.
            ///</summary>
            /// <typeparam name="T"></typeparam>
            /// <param name="item">默认常量</param>
            /// <param name="displayNames">扩展显示名</param>
            /// <returns></returns>
            internal static List<ListItem<int>> ViewList<T>(this T item, params string[] displayNames) where T : Enum
            {
                Type type = typeof(T);
                int i = 0;
                return Enum.GetNames(type).Select(q =>
                {
                    var itm = new ListItem<int>()
                    {
                        Id = Convert.ToInt32(Enum.Parse(type, q)),
                        Name = q,
                        DisplayName = displayNames.Length > i ? displayNames[i] : q,
                        Value = (int)Enum.Parse(type, q),
                        Type = type.FullName,
                        IsDefault = Enum.GetName(type, item)?.Equals(q) ?? false,
                        Icon = "icon-info"
                    };
                    ++i;
                    return itm;
                }).ToList();
            }
    
            /// <summary>
            /// 枚举转换为列表;Id表示当前序号;value表示常量值
            /// DisplayName来源于Display属性
            /// 如果未标识Display属性,则取当前默认名称
            ///</summary>
            /// <typeparam name="T"></typeparam>
            /// <param name="item"></param>
            /// <returns></returns>
            internal static List<ListItem<int>> ViewList<T>(this T item) where T : Enum
            {
                Type type = typeof(T);
                return Enum.GetNames(type).Select(q =>
                {
                    string dname = type.GetField(q)?.GetCustomAttribute<System.ComponentModel.DataAnnotations.DisplayAttribute>()?.Name;
                    dname = !string.IsNullOrEmpty(dname) ? dname : type.GetField(q)?.GetCustomAttribute<System.ComponentModel.DescriptionAttribute>()?.Description;
                    var itm = new ListItem<int>()
                    {
                        Id = Convert.ToInt32(Enum.Parse(type, q)),
                        Name = q,
                        DisplayName = string.IsNullOrEmpty(dname) ? q : dname,
                        Value = Enum.Parse(type, q),
                        Type = type.FullName,
                        IsDefault = Enum.GetName(type, item)?.Equals(q) ?? false,
                        Icon = "icon-info"
                    };
                    return itm;
                }).ToList();
            }
    
            /// <summary>
            /// 通过列表对象,反向获取选中的枚举值
            ///</summary>
            /// <typeparam name="T"></typeparam>
            /// <param name="item"></param>
            /// <returns></returns>
            internal static T EnumValue<T>(this ListItem<int> item) where T : Enum
            {
                return (typeof(T).FullName?.Equals(item.Type) ?? false) ? (T)item.Value : default;
            }
    
            /// <summary>
            /// 获取枚举值的显示名称(未标识则取枚举名称)
            ///</summary>
            /// <param name="item"></param>
            /// <returns></returns>
            internal static string GetDisplay<T>(this T item) where T : Enum
            {
                Type type = typeof(T);
                string f = Enum.GetName(type, item);
                string d = type.GetField(f)?.GetCustomAttribute<System.ComponentModel.DataAnnotations.DisplayAttribute>()?.Name;
                d = !string.IsNullOrEmpty(d) ? d : type.GetField(f)?.GetCustomAttribute<System.ComponentModel.DescriptionAttribute>()?.Description;
                return d ?? f;
            }
    
    
            /// <summary>
            /// 获取枚举值的名称
            /// </summary>
            /// <typeparam name="T"></typeparam>
            /// <param name="item"></param>
            /// <returns></returns>
            internal static string GetName<T>(this T item) where T : Enum
            {
                return Enum.GetName(typeof(T), item);
            }
    
            /// <summary>
            /// 通过枚举值获取实际数值
            /// </summary>
            /// <typeparam name="T"></typeparam>
            /// <param name="item"></param>
            /// <returns></returns>
            internal static int ToInt<T>(this T item) where T : Enum
            {
                return Convert.ToInt32(item);
            }
    
        }
    }
    
    
    
    
  • 相关阅读:
    安装VMware Tools选项显示灰色的正确解决办法
    Other UltraISO 软碟通注册码
    Linux平台Boost 1.6.7的编译方法
    hyper-v显示分辨率如何自动调整
    Ubuntu 14.04下超级终端Minicom连接ARM(转)
    Ubuntu 17.10安装VirtualBox 5.2.2 及相关问题解决
    ffmpeg fails with error "max delay reached. need to consume packet"
    Unity Shader 屏幕后效果——边缘检测
    C++ STL vector容量(capacity)和大小(size)的区别
    Unity影响渲染顺序因素的总结
  • 原文地址:https://www.cnblogs.com/honk/p/14536799.html
Copyright © 2011-2022 走看看