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 ...
随机推荐
- 京东面试:SpringBoot同时可以处理多少请求?
Spring Boot 作为 Java 开发中必备的框架,它为开发者提供了高效且易用的开发工具,所以和它相关的面试题自然也很重要,咱们今天就来看这道经典的面试题:SpringBoot同时可以处理多少个 ...
- 【C# wpf】个人网盘练习项目总结
采用 .net frameowrok 4.5.2 未写持久层代码,不可保存运行时的数据状态.分服务端,客户端,采用tcp通讯,使用了supersocket组件.服务端用winform ,客户端用wpf ...
- 开源云原生平台对比 KubeSphere vs Rainbond
最近因为工作需要,需要找一个功能完善的云原生应用平台,经过自己筛选和朋友推荐,剩下 KubeSphere和Rainbond ,这两个产品都是基于 Kubernetes 之上构建的云原生应用平台,功能都 ...
- 云原生时代的"应用级"多云管理
作者:张齐 当前云计算有多种形态公有云.私有云.边缘云.虚拟机等,如何高效管理多云是当前面临的问题,在云原生时代,又该如何利用云原生技术实现多云管理?本文将讲解通过 Rainbond实现"应 ...
- uniapp 上拉加载下拉刷新
page.json中配置"enablePullDownRefresh": true //单个页面修改刷新按钮的颜色 "app-plus": { "ti ...
- 思迅Pay PC ,WIN7 ,KB3042058
思迅Pay PC ,WIN7, COM , 串口 . 577,驱动. WIN7 SHA256补丁,KB3033929,https://www.microsoft.com/zh-CN/download/ ...
- 在线HMAC加密工具
在线HMAC加密工具提供一站式服务,支持MD5至SHA512.RIPEMD160及SM3等多种哈希算法,用户可便捷选择算法并生成安全的HMAC散列值,确保消息完整性与验证来源.适用于开发调试.网络安全 ...
- FreeRTOS简单内核实现2 双向链表
FreeRTOS Kernel V10.3.1 FreeRTOS 的 list.c / list.h 文件中有 3 个数据结构.2 个初始化函数.2 个插入函数.1 个移除函数和一些宏函数,链表是 F ...
- 效率提升利器:一个在线的.NET源码查询网站
前言 你是否有这样的苦恼,有时候需要查询.NET中的某个类型.方法.属性或程序集的源代码,但又不想从GitHub中下载源代码.今天大姚分享一个在线且实用的.NET源码查询网站. 在线查询地址 http ...
- pytest_terminal_summary重写收集测试报告并发送邮件,报错"Argument(s) {'Config'} are declared in the hookimpl but can not be found in the hookspec"
步骤: 1.conftest.py文件,重写pytest_terminal_summary(terminalreporter, exitstatus, config) 2.run执行pytest.ma ...