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 ...
随机推荐
- C# npoi追加写入时报错 因为文件格式或文件扩展名无效。
造成原因:workbook对象打开后,没有手动close造成的. 使用的npoi版本:2.6.0 ,环境 win10 .net core 5.0
- 电脑临时文件清理2个方法?%temp% cleanmgr
按住电脑快捷键win+R,打开运行框 输入代码 %temp%,点击回车enter或者点击确定,打开temp文件夹[此处存放的都是系统无用的缓存垃圾] 按快捷键Ctrl + A ,点击delete,删除 ...
- k8s核心组件详解和分层架构
k8s核心组件 master中的核心组件 api-server(接口服务,基于rest风格开放k8s接口的服务) kube-controller-manager(管理各个类型的控制器,针对k8s中的各 ...
- Windows文件管理优化-实用电脑软件(一)
RX文件管理器 (稀奇古怪的小软件,我推荐,你点赞!) 日后更新涉及:电脑.维护.清理.小工具.手机.APP.IOS.从WEB.到到UI.从开发,设计:诚意寻找伙伴(文编类.技术类.思想类)共编,共进 ...
- react this指向问题
在JSX事件函数方法中的 this,默认不会绑定 this指向.如果你忘记绑定,当你调用这个函数的时候 this 的值为 undefined.所以使用时一定要绑定好this的指向. 构造方法中绑定 c ...
- OpenSearch 与 Elasticsearch:哪个开源搜索引擎适合您?
当谈论到搜索引擎产品时,Elasticsearch 和 OpenSearch 是两个备受关注的选择.它们都以其出色的功能和灵活性而闻名,但在一些方面存在一些差异.在本文中,我们将从功能和延展性.工具与 ...
- 一种复习flex布局的方法
方法论 flex布局有多个属性,时常会忘记.我们复习的话,单纯看一些博客文章,不能直观的理解,也比较枯燥. 因此如果有一种用写代码闯关的方式来复习(学习)flex布局,那也许会更有意思. FLEXBO ...
- 随机二次元图片API上线
Tips:当你看到这个提示的时候,说明当前的文章是由原emlog博客系统搬迁至此的,文章发布时间已过于久远,编排和内容不一定完整,还请谅解` 随机二次元图片API上线 日期:2017-12-6 阿珏 ...
- Freertos学习:05-内核控制
--- title: rtos-freertos-05-kernel-control date: 2020-06-22 11:10:19 categories: tags: - kernel - fr ...
- paraview安装
apt 安装 sudo apt install paraview 安装包安装 参考 https://blog.csdn.net/weixin_47492286/article/details/1272 ...