zoukankan      html  css  js  c++  java
  • c#枚举转字典或表格

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Reflection;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace CommonLibrary
    {
        /// <summary>
        /// 性别
        /// </summary>
        public enum Sex
        {
            [Description("男")]
            Man = 1,
            [Description("女")]
            Woman = 2
        }
        /// <summary>
        /// 操作日志类型
        /// </summary>
        public enum OperationType
        {
            [Description("新建")]
            Create = 1,
            [Description("删除")]
            Delete = 2,
            [Description("更新")]
            Update = 3
        }
    
        public enum YesNo
        {
            [Description("是")]
            Yes = 1,
            [Description("否")]
            No = 2
        }
    
        /// <summary>
        /// 
        /// </summary>
        public static class EnumHelper
        {
            /// <summary>
            /// 枚举转表格(无需获取说明时使用)
            /// </summary>
            /// <param name="type"></param>
            /// <param name="key"></param>
            /// <param name="value"></param>
            /// <returns></returns>
            public static DataTable EnumToDataTable(Type type, string key = "key", string value = "val")
            {
                string[] Names = System.Enum.GetNames(type);
    
                Array Values = System.Enum.GetValues(type);
    
                DataTable table = new DataTable();
                table.Columns.Add(value, System.Type.GetType("System.String"));
                table.Columns.Add(key, System.Type.GetType("System.Int32"));
                table.Columns[key].Unique = true;
                for (int i = 0; i < Values.Length; i++)
                {
                    DataRow DR = table.NewRow();
                    DR[value] = Names[i].ToString();
                    DR[key] = (int)Values.GetValue(i);
                    table.Rows.Add(DR);
                }
                return table;
            }
    
            /// <summary>
            /// 枚举转表格(需要获取说明时使用)
            /// </summary>
            /// <param name="type"></param>
            /// <param name="key"></param>
            /// <param name="value"></param>
            /// <returns></returns>
            public static DataTable EnumToDataTable<T>(string key = "key", string value = "val")
            {
                Type type = typeof(T);
    
                string[] Names = System.Enum.GetNames(type);
                Array Values = System.Enum.GetValues(type);
                string desc = string.Empty;
    
                DataTable table = new DataTable();
                table.Columns.Add(value, System.Type.GetType("System.String"));
                table.Columns.Add(key, System.Type.GetType("System.Int32"));
                table.Columns[key].Unique = true;
                for (int i = 0; i < Values.Length; i++)
                {
                    T t = (T)System.Enum.Parse(typeof(T), Values.GetValue(i).ToString());
    
                    MemberInfo[] memInfo = type.GetMember(t.ToString());
    
                    if (memInfo != null && memInfo.Length > 0)
                    {
                        object[] attrs = memInfo[0].GetCustomAttributes(typeof(DescriptionAttribute), false);
                        if (attrs != null && attrs.Length > 0)
                        {
                            desc = ((DescriptionAttribute)attrs[0]).Description;
                        }
                    }
    
                    DataRow DR = table.NewRow();
                    DR[value] = string.IsNullOrEmpty(desc) ? Names[i].ToString() : desc
                    DR[key] = (int)Values.GetValue(i);
                    table.Rows.Add(DR);
                }
                return table;
            }
    
            /// <summary>
            /// 枚举转字典(无需获取描述时使用)
            /// </summary>
            /// <param name="type"></param>
            /// <returns></returns>
            public static IDictionary<int, string> EnumToDictionary(Type type)
            {
                string[] Names = System.Enum.GetNames(type);
    
                Array Values = System.Enum.GetValues(type);
    
                IDictionary<int, string> dic = new Dictionary<int, string>();
    
                for (int i = 0; i < Values.Length; i++)
                {
                    dic.Add((int)Values.GetValue(i), Names[i].ToString());
                }
    
                return dic;
            }
    
            /// <summary>
            /// 枚举转字典(需获取描述时使用)
            /// </summary>
            /// <typeparam name="T"></typeparam>
            /// <param name="type"></param>
            /// <returns></returns>
            public static IDictionary<int, string> EnumToDictionary<T>()
            {
                Type type = typeof(T);
    
                string[] Names = System.Enum.GetNames(type);
    
                Array Values = System.Enum.GetValues(type);
    
                IDictionary<int, string> dic = new Dictionary<int, string>();
    
                string desc = string.Empty;
    
                for (int i = 0; i < Values.Length; i++)
                {
                    T t = (T)System.Enum.Parse(typeof(T), Values.GetValue(i).ToString());
    
                    MemberInfo[] memInfo = type.GetMember(t.ToString());
    
                    if (memInfo != null && memInfo.Length > 0)
                    {
                        object[] attrs = memInfo[0].GetCustomAttributes(typeof(DescriptionAttribute), false);
                        if (attrs != null && attrs.Length > 0)
                        {
                            desc = ((DescriptionAttribute)attrs[0]).Description;
                        }
                    }
                    //GetEnumDesc(T);
    
                    dic.Add((int)Values.GetValue(i), string.IsNullOrEmpty(desc) ? Names[i].ToString() : desc);
                }
    
                return dic;
            }
    
        }
    }
    

      

  • 相关阅读:
    设计模式-创建型-原型模式
    设计模式-创建型-抽象工厂模式
    设计模式-创建型-工厂模式
    设计模式-创建型-单例模式
    css3技巧属性之text-overflow
    bootstrap如何自定义5等分
    LeetCode-64-最小路径和
    LeetCode-62-不同路径
    LeetCode-5-最长回文子串
    LeetCode-98-验证二叉搜索树
  • 原文地址:https://www.cnblogs.com/zhoushangwu/p/9581049.html
Copyright © 2011-2022 走看看