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;
} }
}
c#枚举转字典或表格的更多相关文章
- OSS.Common获取枚举字典列表标准库支持
上篇(.Net Standard扩展支持实例分享)介绍了OSS.Common的标准库支持扩展,也列举了可能遇到问题的解决方案.由于时间有限,同时.net standard暂时还没有提供对Descrip ...
- Vue 利用后端的数据字典和Map对象实现表格列字段动态转义的处理方案
1.前言 Vue中,使用el-table组件,经常遇到列字段转义的问题.常规处理方法有以下两种: 方法1:在模板中使用v-if,直接转义.如: <el-table-column label= ...
- c#枚举使用详解
简介 1. 枚举(enum type)通常用来表示一组常量.由于枚举是强类型的,这在编程中给我们提供了极大的方便. 2. 枚举的定义: public enum Sex { 男 = 0, 女 = 1 } ...
- C# 添加枚举中文资源
在业务开发过程中,添加枚举,在固定枚举值的同时,也需要中文的文案. 如果不想添加语言资源项.添加枚举转语资源项,可以使用特性标记. 属性描述 DescriptionAttribute 先看案例: pu ...
- ASP.NET MVC 枚举类型转LIST CONTROL控件
在实际应用中,我们经常会用到下拉框.多选.单选等类似的控件,我们可以统称他们为List Control,他们可以说都是一种类型的控件,相同之处都是由一个或一组键值对的形式的数据进行绑定渲染而成的. 这 ...
- C#.NET MVC 枚举转dictionary自动装载生成下拉框
/// <summary> /// 枚举转SelectListItem /// </summary> public class Enum_Helper { /// < ...
- 爬取表格类网站数据并保存为excel文件
本文转载自以下网站:50 行代码爬取东方财富网上市公司 10 年近百万行财务报表数据 https://www.makcyun.top/web_scraping_withpython6.html 主要学 ...
- [ PyQt入门教程 ] PyQt5中数据表格控件QTableWidget使用方法
如果你想让你开发的PyQt5工具展示的数据显得整齐.美观.好看,显得符合你的气质,可以考虑使用QTableWidget控件.之前一直使用的是textBrowser文本框控件,数据展示还是不太美观.其中 ...
- [WinForm] DataGridView 绑定 DT && ComboBox 列绑定 Dict
一 需求介绍 一般像枚举类型的数据,我们在数据库里存储着诸如(1.2.3.4-)或者("001"."002"."003"-)此类,但是界面 ...
随机推荐
- JFrame的getContentPane
我们可以在 JFrame 对象中添加 AWT 或者 Swing 组件.但是,虽然它有 add 方法,却不能直接用于添加组件,否则会抛出异常.造成这个现象的原因只有一个解释:JFrame 不是一个容器, ...
- eclipse里新建work set,将项目分组放在不同文件夹
想必大家的Eclipse里也会有这么多得工程...... 每次工作使用到的项目肯定不会太多...... 每次从这么大数量的工程当中找到自己要使用的, 必须大规模的滚动滚动条......有点不和谐了. ...
- Cmake知识----编写CMakeLists.txt文件编译C/C++程序(转)
1.CMake编译原理 CMake是一种跨平台编译工具,比make更为高级,使用起来要方便得多.CMake主要是编写CMakeLists.txt文件,然后用cmake命令将CMakeLists.txt ...
- np.vstack与np.hstack
转自:https://zhuanlan.zhihu.com/p/82996332 留作备忘
- Github搜索技巧
按仓库名称.说明或自述文件内容搜索 通过 in 限定符,您可以将搜索限制为仓库名称.仓库说明.自述文件内容或这些的任意组合. 如果省略此限定符,则只搜索仓库名称和说明. 限定符 示例 in:name ...
- Linux运维--15.OpenStack vm使用keepalived 实现负载均衡
外接mariadb集群 实现负载均衡 实验环境 10.0.1.27 galera1 10.0.1.6 galera2 10.0.1.23 galera3 10.0.1.17 harpoxy1 hapr ...
- Android Binder实现浅析-Binder驱动
简介 Android是如何实现跨进程通信的,大家熟悉的Binder是什么,怎么设计的,进程间的数据如何发送接收的.本文将以及解析,并对Binder驱动实现.Native层实现.Java层实现三块做一个 ...
- 全面解析百度大脑发布“AI开发者‘战疫’守护计划”
即日起,百度大脑发布“AI开发者战疫守护计划” 大疫当前,人人有责,携手开发者共同出击抗击疫情 基于百度大脑AI开放平台和飞桨深度学习平台,积极运用算法.算力.软件等“武器”助力抗疫! 谁能参与计 ...
- Windows、Linux之间传输文件的几种方式
常见的文件传输协议有ftp.sftp,sftp就是在ftp的基础上对传输的数据进行了加密. ftp速度快,sftp速度略慢但安全性高. ftp默认使用21端口,sftp默认使用22端口. 我使用的是C ...
- Codeforces Round 450 D 隔板法+容斥
题意: Count the number of distinct sequences a1, a2, ..., an (1 ≤ ai) consisting of positive integers ...