C# 枚举帮助类EnumHelper(获取描述、名称和数值)
帮助类定义
public class EnumHelper
{
#region 静态方法
public static Dictionary<string, string> GetEnumDescription<T>()
{
Dictionary<string, string> dic = new Dictionary<string, string>();
FieldInfo[] fields = typeof(T).GetFields();
foreach (FieldInfo field in fields)
{
if (field.FieldType.IsEnum)
{
object[] attr = field.GetCustomAttributes(typeof(DescriptionAttribute), false);
string description = attr.Length == 0 ? field.Name : ((DescriptionAttribute)attr[0]).Description;
dic.Add(field.Name, description);
}
}
return dic;
}
/// <summary>
/// 获取对应的枚举描述
/// </summary>
public static List<KeyValuePair<string, string>> GetEnumDescriptionList<T>()
{
List<KeyValuePair<string, string>> result = new List<KeyValuePair<string, string>>();
FieldInfo[] fields = typeof(T).GetFields();
foreach (FieldInfo field in fields)
{
if (field.FieldType.IsEnum)
{
object[] attr = field.GetCustomAttributes(typeof(DescriptionAttribute), false);
string description = attr.Length == 0 ? field.Name : ((DescriptionAttribute)attr[0]).Description;
result.Add(new KeyValuePair<string, string>(field.Name, description));
}
}
return result;
}
/// <summary>
/// 获取枚举的 值和描述
/// </summary>
/// <typeparam name="T"></typeparam>
/// <returns></returns>
public static List<KeyValuePair<int, string>> GetEnumValueDescriptionList<T>()
{
List<KeyValuePair<int, string>> result = new List<KeyValuePair<int, string>>();
FieldInfo[] fields = typeof(T).GetFields();
foreach (FieldInfo field in fields)
{
if (field.FieldType.IsEnum)
{
object[] attr = field.GetCustomAttributes(typeof(DescriptionAttribute), false);
string description = attr.Length == 0 ? field.Name : ((DescriptionAttribute)attr[0]).Description;
result.Add(new KeyValuePair<int, string>(Convert.ToInt32(field.GetValue(null)), description));
}
}
return result;
}
public static string GetDescriptionByEnumName<T>(string name)
{
try
{
Dictionary<string, string> dic = GetEnumDescription<T>();
string description = dic[name];
return description;
}
catch (Exception ex)
{
return "";
}
}
#endregion
}
控件绑定
public enum GenderType
{
[Description("男")]
Man = 1,
[Description("女")]
Woman = 2
}
private void frmMain_Load(object sender, EventArgs e)
{
comboBox1.DataSource = EnumHelper.GetEnumValueDescriptionList<GenderType>().Select(x => new
{
Key = x.Value,
Value = x.Key
}).ToList();
comboBox1.DisplayMember = "Key";
comboBox1.ValueMember = "Value";
}
C# 枚举帮助类EnumHelper(获取描述、名称和数值)的更多相关文章
- .NET--------枚举扩展方法(枚举转list,获取枚举描述)
/// <summary> /// get enum description by name /// </summary> /// <typeparam name=&qu ...
- 获取枚举类型Description特性的描述信息
C#中可以对枚举类型用Description特性描述. 如果需要对Description信息获取,那么可以定义一个扩展方法来实现.代码如下: public static class EnumExten ...
- Delphi中获取某类的祖先类及其所在单元名称(使用GetTypeData(PClass.ClassInfo)函数,并且该类是从TPersistent类的派生类才可以这么使用)
前几天在CSDN社区看到一篇<如何得到自身单元名称>的帖子,其中一位名为sdzeng网友给出了答案.受此启发,自己写了一个函数,用来获取指定类的所有祖先类的名称及其所在的单元名称. //参 ...
- Android 获取版本号名称工具类
package com.example.grenaderose.redthunder.utils; import android.content.Context; import android.con ...
- C# - 获取类中属性的名称
用反射控制的,不过获取属性名称的方法,用方法形式获取的,不知道消耗大不大 using System; using System.Collections.Generic; using System.Li ...
- System.Reflection 获取描述
我们需要获取类,属性,方法的描述.这个跟获取枚举的描述一样,需要我们通过反射来做.这还需要我们的利用System.ComponentModel:Description 的属性来完成. 新建一个类:使 ...
- 【C#公共帮助类】枚举独特类
这个是枚举类,可能大家根据个人需求不同,不是很需要,但是跟着做那个项目的朋友会用到 我在这贴一下代码 using System; using System.Collections.Generic; u ...
- QMetaEnum利用Qt元数据实现枚举(enum)类型值及字符串转换
版权声明:若无来源注明,Techie亮博客文章均为原创. 转载请以链接形式标明本文标题和地址: 本文标题:QMetaEnum利用Qt元数据实现枚举(enum)类型值及字符串转换 本文地址:ht ...
- king枚举帮助类
可以方便的实现枚举 枚举 public enum DeptType { [Description("科室1")] Professional = , [Description(&qu ...
- NX二次开发-Block UI C++界面Enumeration(枚举)控件的获取(持续补充)
NX9+VS2012 public: void SetBlockUIShow(); void EnumInt::SetBlockUIShow() { //获取枚举控件 PropertyList* En ...
随机推荐
- 行列式求值,从 $n!$ 优化到 $n^3$
前置知识 \(\sum\) 为累加符号,\(\prod\) 为累乘符号. 上三角矩阵指只有对角线及其右上方有数值其余都是 \(0\) 的矩阵. 如果一个矩阵的对角线全部为 \(1\) 那么这个矩阵为单 ...
- nginx aio模块添加与配置
1. 升级目的 让现有服务平滑过渡到高版本,减少服务漏洞,提高服务性能 让其支持nginx最新特性 nginx threads模块 2. 获取nginx1.7.2版本 wget http://ngin ...
- cmake之find_library使用问题
附上工程源码 demo工程 PS:这个工程用于导出库 CMakeLists.txt cmake_minimum_required(VERSION 3.5) project(demo LANGUAGES ...
- codemirror-editor-vue3 编辑器只要换行保存就会有小红点
先看示例 找了篇博客写的贼详细 https://blog.csdn.net/xujingyiss/article/details/118992763 只要设置这二个 let conten ...
- react props-type
对于组件来说,props是外部传入的,无法保证组件使用者传入什么格式的数据,简单来说就是组件调用者可能不知道组件封装着需要什么样的数据,如果传入的数据不对,可能会导致程序异常,所以必须要对于props ...
- CF1836
A.Destroyer 开个桶记录个数,看满不满足单调不上升即可. B.Astrophysicists 辛辛苦苦写了这么久的文章就没了????烦死了. 自己做 Virtual Contest 的时候这 ...
- 鸿蒙HarmonyOS实战-窗口管理
前言 窗口管理是指计算机操作系统中管理和控制窗口的一种机制.窗口管理器负责处理窗口的创建.关闭.移动.调整大小等操作,并且决定窗口的位置.层级.是否可见.是否接收用户输入等属性.窗口管理器还负责绘制窗 ...
- ssh练习
根据要求完成部署 根据如下要求,完成部署过程 1.恢复7.8.9.31.41所有机器的快照 7 8 9 web服务 nginx 172.16.1.xx nfs-31 提供共享文件存储 ...
- Sqlite windows11 安装与使用
首先进入Sqlite官方网址: https://www.sqlite.org/download.html 然后下载下面框起来的两个压缩文件 下载完成后解压 接下来去配置环境变量,右键此电脑->属 ...
- Springcloud开发之OpenFeign调用和认证
SpringCloud开发cloud具有巨大的灵活性. 在调用其它服务的时候有多种方式,虽然本质一样,但是细节还是有所差异. 一.概述 当a服务调用b服务的时候有多种方式进行: 1.通过openFei ...