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

  

c#枚举转字典或表格的更多相关文章

  1. OSS.Common获取枚举字典列表标准库支持

    上篇(.Net Standard扩展支持实例分享)介绍了OSS.Common的标准库支持扩展,也列举了可能遇到问题的解决方案.由于时间有限,同时.net standard暂时还没有提供对Descrip ...

  2. Vue 利用后端的数据字典和Map对象实现表格列字段动态转义的处理方案

    1.前言   Vue中,使用el-table组件,经常遇到列字段转义的问题.常规处理方法有以下两种: 方法1:在模板中使用v-if,直接转义.如: <el-table-column label= ...

  3. c#枚举使用详解

    简介 1. 枚举(enum type)通常用来表示一组常量.由于枚举是强类型的,这在编程中给我们提供了极大的方便. 2. 枚举的定义: public enum Sex { 男 = 0, 女 = 1 } ...

  4. C# 添加枚举中文资源

    在业务开发过程中,添加枚举,在固定枚举值的同时,也需要中文的文案. 如果不想添加语言资源项.添加枚举转语资源项,可以使用特性标记. 属性描述 DescriptionAttribute 先看案例: pu ...

  5. ASP.NET MVC 枚举类型转LIST CONTROL控件

    在实际应用中,我们经常会用到下拉框.多选.单选等类似的控件,我们可以统称他们为List Control,他们可以说都是一种类型的控件,相同之处都是由一个或一组键值对的形式的数据进行绑定渲染而成的. 这 ...

  6. C#.NET MVC 枚举转dictionary自动装载生成下拉框

      /// <summary> /// 枚举转SelectListItem /// </summary> public class Enum_Helper { /// < ...

  7. 爬取表格类网站数据并保存为excel文件

    本文转载自以下网站:50 行代码爬取东方财富网上市公司 10 年近百万行财务报表数据 https://www.makcyun.top/web_scraping_withpython6.html 主要学 ...

  8. [ PyQt入门教程 ] PyQt5中数据表格控件QTableWidget使用方法

    如果你想让你开发的PyQt5工具展示的数据显得整齐.美观.好看,显得符合你的气质,可以考虑使用QTableWidget控件.之前一直使用的是textBrowser文本框控件,数据展示还是不太美观.其中 ...

  9. [WinForm] DataGridView 绑定 DT && ComboBox 列绑定 Dict

    一  需求介绍 一般像枚举类型的数据,我们在数据库里存储着诸如(1.2.3.4-)或者("001"."002"."003"-)此类,但是界面 ...

随机推荐

  1. java8 Stream API笔记

    生成Stream Source的方式 从Collection和数组生成 * Collection.stream() * Collection.parallelStream() * Arrays.str ...

  2. NR / 5G - The Round Robin algorithm

  3. sql 忘记密码 解决方法(window cmd命令解决)

    cd wamp\bin\mysql\mysql5.6.17\bin mysqld --skip-grant-tables

  4. windows本地安装以及使用Jenkins

    首先保证本地已安装JDK,Jenkins是需要Java开发的,需要JDK运行环境,此处不提供,网上一大把. 一.Jenkin安装: 下载Jekins,到官网下载,https://jenkins.io/ ...

  5. lwip nd没有实现ra,contik有参考

    lwip中关于nd的实现,没有路由器的功能,不能发送ra 在contiki中发现有nd发送ra的实现, contiki/core/net/ipv6/uip-ds6.c 在rs的接收处理中,发送soll ...

  6. HTML,Css,JavaScript之间的关系

    简述HTML,Css,JavaScript 网页设计思路是把网页分成三个层次,即:结构层(HTML).表示层(CSS).行为层(Javascript). 1.HTML(超文本标记语言 Hyper Te ...

  7. PHP关于mb_substr不能起作用的问题

    mb_substr不能起作用最大的原因是因为没有在php.ini文件没有把 ;extension=mbstring 前面的 :号去掉

  8. JS从键盘输入当月利润,求应发放奖金总数

    题目:企业发放的奖金根据利润提成.利润(I)低于或等于10万元时,奖金可提10%:利润高 于10万元,低于20万元时,低于10万元的部分按10%提成,高于10万元的部分,可可提 成7.5%:20万到4 ...

  9. Spring(七)核心容器 - 钩子接口

    目录 前言 1.Aware 系列接口 2.InitializingBean 3.BeanPostProcessor 4.BeanFactoryPostProcessor 5.ImportSelecto ...

  10. 05.JS函数

    前言: 学习一门编程语言的基本步骤(01)了解背景知识(02)搭建开发环境(03)语法规范(04)常量和变量(05)数据类型(06)数据类型转换(07)运算符(08)逻辑结构(09)函数9.函数——f ...