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 ...
随机推荐
- 2024-05-22:用go语言,你有一个包含 n 个整数的数组 nums。 每个数组的代价是指该数组中的第一个元素的值。 你的目标是将这个数组划分为三个连续且互不重叠的子数组。 然后,计算这三个子数
2024-05-22:用go语言,你有一个包含 n 个整数的数组 nums. 每个数组的代价是指该数组中的第一个元素的值. 你的目标是将这个数组划分为三个连续且互不重叠的子数组. 然后,计算这三个子数 ...
- 将python文件转换成exe可执行文件
一.安装Pyinstaller pip install pyinstaller(Pyinstaller) 二.找到 .py文件的路径并执行如下命令 pyinstaller -F 要转换的文件.py 三 ...
- golang beego 使用supervisor 部署后台进程管理. 静态文件找不到的解决办法.
directory=/root/go/src/you_self_dir 请在客户端配置文件*.ini中加入一行命令, 等于号后面就是自己的项目目录,这时就能找到项目文件了.
- .NET Core 项目Linux环境下生成二维码
问题: 公司系统开发中,需要对企微授权链接进行二维码生成,然后向客户提供:当然,首当其冲想到的是使用ZXing.NET库进行实现,毕竟生成简单二维码也就那几句代码:然而,在本地环境中,一切都很正常,但 ...
- nginx37条优化
nginx优化: 1. cpu:核心配置 方法1: worker_processes auto; 自动调用[推荐] 方法2: worker_processes 4; 手工配置 检查CPU核心:yum ...
- 椭圆曲线密码学(ECC)加解密,附带python代码
想起来很久没写博客了,刚好今天要写实验报告,随便把之前的也完成吧 1.椭圆曲线概念 椭圆曲线在经过化解后,可以用这条式子表达:E:y²=x³+ax+b 其背后的密码学原理,是基于椭圆曲线离散对数问题, ...
- [SWPUCTF 2021 新生赛]gift_F12
首先我们打开环境会发现花里胡哨的,而题目中有提示:F12,所以我们直接F12查看源码 然后ctrl+f信息检索flag.直接找到flag提交 但要注意提交格式为NSSCTF{}
- 程序员面试金典-面试题 16.20. T9键盘
题目: 在老式手机上,用户通过数字键盘输入,手机将提供与这些数字相匹配的单词列表.每个数字映射到0至4个字母.给定一个数字序列,实现一个算法来返回匹配单词的列表.你会得到一张含有有效单词的列表.映射如 ...
- PasteSpider的集群组件PasteCluster(让你的项目快速支持集群模式)的思路及实现(含源码)
PasteSpider是什么? 一款使用.net编写的开源的Linux容器部署助手,支持一键发布,平滑升级,自动伸缩, Key-Value配置,项目网关,环境隔离,运行报表,差量升级,私有仓库,集群部 ...
- Spring源码——ConfigurationClassPostProcessor类
引言 Spring容器中提供很多方便的注解供我们在工作中使用,比如@Configuration注解,里面可以在方法上定义@Bean注解,将调用方法返回的对象交由Bean容器进行管理,那么Spring框 ...