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;
            }
    
        }
    }
    

      

  • 相关阅读:
    Tensor:Pytorch神经网络界的Numpy
    你真的懂语音特征吗?
    ES高级(17) 使用基础(5)安装(5) Linux 集群
    ES高级(16) 使用基础(4)安装(4) Linux 单机
    ES高级(15) 使用基础(3)安装(3) Windows 集群
    ES高级(14) 使用基础(2)安装(2) 概念
    ES入门 (13)Java API 操作(4)DQL(1) 请求体查询/term 查询,查询条件为关键字/分页查询/数据排序/过滤字段/Bool 查询/范围查询/模糊查询/高亮查询/聚合查询/分组查询
    ES入门 (12)Java API 操作(3)DML 新增文档/修改文档/查询文档/删除文档/批量操作
    ES入门 (11)Java API 操作(2)DDL 索引操作
    ES入门 (10)Java API 操作(1)准备
  • 原文地址:https://www.cnblogs.com/zhoushangwu/p/9581049.html
Copyright © 2011-2022 走看看