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"-)此类,但是界面 ...
随机推荐
- 在家想自学Java,有C语言底子,请问哪本书适合?
一.问题剖析 看到这个问题,我想吹水两句再做推荐.一般发出这个疑问都处在初学编程阶段,编程语言都是相通的,只要你领悟了一门语言的"任督二脉",以后你学哪一门语言都会轻易上手.学语言 ...
- 第3章 JDK并发包(五)
3.3 不要重复发明轮子:JDK的并发容器 3.3.1 超好用的工具类:并发集合简介 JDK提供的这些容器大部分在java.util.concurrent包中. ConcurrentHashMap:这 ...
- Python3(六) 面向对象
一.类的定义 1. class Student(): name = ' ' #定义变量 age = 0 def print_file(self): #定义函数 ...
- 《自拍教程6》打开Windows文件后缀
如果你用的是Windows操作系统, 请把文件后缀名打开,千万别隐藏后缀名, 后续有各类 .py, .sh, .bat, .exe等不同文件, 需要你时刻关注文件后缀名. 如果是我看到我手下的测试人员 ...
- space transport protocols
VSAT系统对TCP的改进 https://www.vsat-systems.com/broadband-satellite-internet/index.html TCP/IP over satel ...
- 动态规划------背包问题(c语言)
/*背包问题: 背包所能容纳重量为10:共五件商品,商品重量用数组m存储m[5]={2,2,6,5,4}, 每件商品的价值用数组n存储,n[5]={6,3,5,4,6};求背包所能装物品的最大价值. ...
- centos7使用MySQL的Yum存储库安装mysql5.7.27
下载yum源 官网地址:http://dev.mysql.com/downloads/repo/yum/ centos7系统: http://dev.mysql.com/get/mysql57-com ...
- c# 匿名方法(函数) 匿名委托 内置泛型委托 lamada
匿名方法:通过匿名委托 .lamada表达式定义的函数具体操作并复制给委托类型: 匿名委托:委托的一种简单化声明方式通过delegate关键字声明: 内置泛型委托:系统已经内置的委托类型主要是不带返回 ...
- getElementsByTagName得到的对象
今天练习使用DOM创建html元素,想通过getElementsByTagName("body")获得body对象,然后建立和新创建的元素的关系,如下图: 其实,getElemen ...
- Android Studio 学习笔记(五):WebView 简单说明
Android中一个用于网页显示的控件,实际上,也可以看做一个功能最小化的浏览器,看起来类似于在微信中打开网页链接的页面.WebView主要用于在app应用中方便地访问远程网页或本地html资源.同时 ...