C# EnumHelper
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Reflection;
- namespace Utils
- {
- /// <summary>
- /// 枚举帮助类
- /// </summary>
- public class EnumHelper
- {
- /// <summary>
- /// 返回枚举值的描述信息。
- /// </summary>
- /// <param name="value">要获取描述信息的枚举值。</param>
- /// <returns>枚举值的描述信息。</returns>
- public static string GetEnumDesc<T>(int value)
- {
- Type enumType = typeof(T);
- DescriptionAttribute attr = null;
- // 获取枚举常数名称。
- string name = Enum.GetName(enumType, value);
- if (name != null)
- {
- // 获取枚举字段。
- FieldInfo fieldInfo = enumType.GetField(name);
- if (fieldInfo != null)
- {
- // 获取描述的属性。
- attr = Attribute.GetCustomAttribute(fieldInfo, typeof(DescriptionAttribute), false) as DescriptionAttribute;
- }
- }
- // 返回结果
- if (attr != null && !string.IsNullOrEmpty(attr.Description))
- return attr.Description;
- else
- return string.Empty;
- }
- /// <summary>
- /// 返回枚举项的描述信息。
- /// </summary>
- /// <param name="e">要获取描述信息的枚举项。</param>
- /// <returns>枚举项的描述信息。</returns>
- public static string GetEnumDesc(Enum e)
- {
- if (e == null)
- {
- return string.Empty;
- }
- Type enumType = e.GetType();
- DescriptionAttribute attr = null;
- // 获取枚举字段。
- FieldInfo fieldInfo = enumType.GetField(e.ToString());
- if (fieldInfo != null)
- {
- // 获取描述的属性。
- attr = Attribute.GetCustomAttribute(fieldInfo, typeof(DescriptionAttribute), false) as DescriptionAttribute;
- }
- // 返回结果
- if (attr != null && !string.IsNullOrEmpty(attr.Description))
- return attr.Description;
- else
- return string.Empty;
- }
- /// <summary>
- /// 获取枚举描述列表,并转化为键值对
- /// </summary>
- /// <typeparam name="T"></typeparam>
- /// <param name="isHasAll">是否包含“全部”</param>
- /// <param name="filterItem">过滤项</param>
- /// <returns></returns>
- public static List<EnumKeyValue> EnumDescToList<T>(bool isHasAll, params string[] filterItem)
- {
- List<EnumKeyValue> list = new List<EnumKeyValue>();
- // 如果包含全部则添加
- if (isHasAll)
- {
- list.Add(new EnumKeyValue() { Key = , Name = "全部" });
- }
- #region 方式一
- foreach (var item in typeof(T).GetFields())
- {
- // 获取描述
- var attr = item.GetCustomAttribute(typeof(DescriptionAttribute), true) as DescriptionAttribute;
- if (attr != null && !string.IsNullOrEmpty(attr.Description))
- {
- // 跳过过滤项
- if (Array.IndexOf<string>(filterItem, attr.Description) != -)
- {
- continue;
- }
- // 添加
- EnumKeyValue model = new EnumKeyValue();
- model.Key = (int)Enum.Parse(typeof(T), item.Name);
- model.Name = attr.Description;
- list.Add(model);
- }
- }
- #endregion
- #region 方式二
- //foreach (int item in Enum.GetValues(typeof(T)))
- //{
- // // 获取描述
- // FieldInfo fi = typeof(T).GetField(Enum.GetName(typeof(T), item));
- // var attr = fi.GetCustomAttribute(typeof(DescriptionAttribute), false) as DescriptionAttribute;
- // if (attr != null && !string.IsNullOrEmpty(attr.Description))
- // {
- // // 跳过过滤项
- // if (Array.IndexOf<string>(filterItem, attr.Description) != -1)
- // {
- // continue;
- // }
- // // 添加
- // EnumKeyValue model = new EnumKeyValue();
- // model.Key = item;
- // model.Name = attr.Description;
- // list.Add(model);
- // }
- //}
- #endregion
- return list;
- }
- /// <summary>
- /// 获取枚举值列表,并转化为键值对
- /// </summary>
- /// <typeparam name="T"></typeparam>
- /// <param name="isHasAll">是否包含“全部”</param>
- /// <param name="filterItem">过滤项</param>
- /// <returns></returns>
- public static List<EnumKeyValue> EnumToList<T>(bool isHasAll, params string[] filterItem)
- {
- List<EnumKeyValue> list = new List<EnumKeyValue>();
- // 如果包含全部则添加
- if (isHasAll)
- {
- list.Add(new EnumKeyValue() { Key = , Name = "全部" });
- }
- foreach (int item in Enum.GetValues(typeof(T)))
- {
- string name = Enum.GetName(typeof(T), item);
- // 跳过过滤项
- if (Array.IndexOf<string>(filterItem, name) != -)
- {
- continue;
- }
- // 添加
- EnumKeyValue model = new EnumKeyValue();
- model.Key = item;
- model.Name = name;
- list.Add(model);
- }
- return list;
- }
- }
- /// <summary>
- /// 枚举键值对
- /// </summary>
- public class EnumKeyValue
- {
- public int Key { get; set; }
- public string Name { get; set; }
- }
- }
C# EnumHelper的更多相关文章
- EnumHelper.cs枚举助手(枚举描述信息多语言支持)C#
C#里面经常会用到枚举类型,枚举是值类型对象,如果你想用枚举类型的多属性特性,或者你想在MVC页面上通过简单的值类型转换,将某字段值所代表的含义转换为文字显示,这时候必须要将枚举扩展,是它支持文本描述 ...
- EnumHelper枚举常用操作类
在项目中需要把枚举填充到下拉框中,所以使用统一的方法实现,测试代码如下: namespace CutPictureTest.Comm { public class EnumHelper { publi ...
- 在C#编程中玩转枚举,分享我的EnumHelper。
在C#编程中玩转枚举,分享我的EnumHelper. 在软件开发过程中,我们经常会为特定的场景下的特定数据定义逻辑意义.比如在用户表中,我们可能会有一个用户状态字段,该字段为整形.如果该字段的值为1则 ...
- C# EnumHelper Enum的值,Description,ToString()的相互转换
首先定义枚举类型,如下: /// <summary> /// 板块 /// </summary> public enum Plate { [Descriptio ...
- EnumHelper.cs
网上找的,还比较实用的: using System; using System.Collections.Generic; using System.ComponentModel; using Syst ...
- .net工具类
ConvertHelper public class ConvertHelper { /// <summary> /// 转换类型 /// </summary> /// < ...
- c#枚举使用详解
简介 1. 枚举(enum type)通常用来表示一组常量.由于枚举是强类型的,这在编程中给我们提供了极大的方便. 2. 枚举的定义: public enum Sex { 男 = 0, 女 = 1 } ...
- grape动态PHP结构(二)——管理后台
一.概述
- 利用SCORE法则来总结一次偷懒的单元测试过程
最近遇到一个单元测试的问题,本周正好学个了一个SCORE法则,这里正好练练手应用此法则将问题的前因后果分享给大家. S:背景 代码要有单元测试,检测的标准就是统计代码的单元测试覆盖率,程序员需要达到 ...
随机推荐
- 仿async/await(一)and Gulp:新一代前端构建利器
NET 4.5的async/await真是个神奇的东西,巧妙异常以致我不禁对其实现充满好奇,但一直难以窥探其门径.不意间读了此篇强文<Asynchronous Programming in C# ...
- WCF Restful Service的服务
构建基于WCF Restful Service的服务 前言 传统的Asmx服务,由于遵循SOAP协议,所以返回内容以xml方式组织.并且客户端需要添加服务端引用才能使用(虽然看到网络上已经提供了这方面 ...
- c/c++中typedef详解
1. typedef 最简单使用 typedef long byte_4; // 给已知数据类型long起个新名字,叫byte_4 你可以在任何需要 long 的上下文中使用 byte_4.注意 ty ...
- TFS二次开发的数据统计以PBI、Bug、Sprint等为例(一)
TFS二次开发的数据统计以PBI.Bug.Sprint等为例(一) 在TFS二次开发中,我们可能会根据某一些情况对各个项目的PBI.BUG等工作项进行统计.在本文中将大略讲解如果进行这些数据统计. 一 ...
- Microsoft Message Analyzer (微软消息分析器,“网络抓包工具 - Network Monitor”的替代品)官方正式版现已发布
Microsoft Message Analyzer (微软消息分析器,“网络抓包工具 - Network Monitor”的替代品)官方正式版现已发布 来自官方日志的喜悦 被誉为全新开始的消息分析器 ...
- [转载]linux下编译php中configure参数具体含义
编译N次了 原来这么回事 原文地址:linux下编译php中configure参数具体含义作者:捷心特 php编译参数的含义 ./configure –prefix=/usr/local/php ...
- OpenCV 4 Python高级配置—安装setuptools,matplotlib,six,dateutil,pyparsing 完整过程
Matplotib 是python 的一个绘图库,里头有各种各样的绘图方法,可以用Matplotib 显示图像,放大图像,保存图像等等,对于OpenCV处理图像具有非常大的帮助.但是,安装Matplo ...
- Arduino 各种模块篇 GPRS module 手机模块 短信 电话 上网 for texting, calling, internet
---恢复内容开始--- The GPRS shield which I tested is one which looks like this: ---恢复内容结束--- Need to be re ...
- flask 真是太棒啦,阅读手册后就能做出一个博客了
真是很好的东西,有很多有益处的东西. 有template引擎, 有flask自己带的g (用来处理访问与数据库打开关闭的) 有flask自己的处理session的功能 自带的jinja2模板引擎也是比 ...
- Spring.Net-DI依赖注入和Ioc控制反转
Spring.Core作为整个Spring框架的基础,实现了依赖注入的功能.Spring框架的其它模块都要依赖或扩展该模块. IObjectFactory接口,该接口实现了工厂模式,使用它可以帮我们创 ...