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. 关于在osgearth 中 出现 arial.ttf : file not handled 的问题

    这是由于配置osg时 freetype 插件没有配置到位. 我个人的解决方法 打开CMAKE,点击advance,不勾选OSG_TEXT_USE_FONTCONFIG. 同时将freetype路径设置 ...

  2. postman之存储测试结果

    前言 在Jmeter的随笔中,我跟大家讲过利用Jmeter工具存储测试结果,那么,postman工具要该如何存储测试结果呢?下面一起来学习吧! 一:添加一个登录请求,填入接口参数点击send 二:点击 ...

  3. MySQL读写分离---Mycat

    一.什么是读写分离 在数据库集群架构中,让主库负责处理事务性查询,而从库只负责处理select查询,让两者分工明确达到提高数据库整体读写性能.当然,主数据库另外一个功能就是负责将事务性查询导致的数据变 ...

  4. Python3 (一) 基本类型

    前言: 什么是代码? 代码是现实世界事物在计算机世界中的映射. 什么事写代码? 写代码是将现实世界中的事物用计算机语言来描述. 一.数字:整形与浮点型 整型:int 浮点型:float (没有单精度和 ...

  5. Apache缓存相关配置

    小编今天来总结下 apache的缓存模块相关信息 硬盘缓存:mod_disk_cache,依赖 mod_cache 模块 内存缓存:mod_mem_cache,依赖 mod_cache 模块 文件缓存 ...

  6. Linux文本界面字体颜色修改

    环境 基于centos 6.5 在文本界面 系统目录的字体颜色是 黑底蓝字  严重看不清楚,对此作出修改 使用 vi 编辑   进入  /etc/DIR_COLORS 找到“DIR 01;34   # ...

  7. Admin后台权限管理、三大认证

    目录 APIView的请求生命周期 三大认证规则 权限六表 自定义User表 详细配置演示 models.py setting.py admin.py 使用过程: 控制填写信息的字段 控制添加权限 控 ...

  8. WebAPI中的定时处理-使用Quartz.Net

    借鉴: https://blog.csdn.net/lordwish/article/details/78926252 在最近的一篇文章中讲到了如何在web API中实现定时处理,采用的是比较原始的T ...

  9. 如何利用border书写三角形,建议考虑正方形

    网页做三角形图片,你还在拿ps调整吗?out了,老铁,来和我一起脑海畅想一个正方形是由4个等腰直角三角形构成,然后我想保留上边的三角形,那下边.左边.右边的三角形就没了(设置背景色transparen ...

  10. SAP MM 一个含有多个账号分配对象的行项目的PO及其收货

    SAP MM 一个含有多个账号分配对象的行项目的PO及其收货 如下的采购订单,一个行项目数量为8PC,分别对应8个固定资产号, 在该ITEM的科目分配里,按数量做了拆分,每个数量对应一个固定资产号.如 ...