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的更多相关文章

  1. EnumHelper.cs枚举助手(枚举描述信息多语言支持)C#

    C#里面经常会用到枚举类型,枚举是值类型对象,如果你想用枚举类型的多属性特性,或者你想在MVC页面上通过简单的值类型转换,将某字段值所代表的含义转换为文字显示,这时候必须要将枚举扩展,是它支持文本描述 ...

  2. EnumHelper枚举常用操作类

    在项目中需要把枚举填充到下拉框中,所以使用统一的方法实现,测试代码如下: namespace CutPictureTest.Comm { public class EnumHelper { publi ...

  3. 在C#编程中玩转枚举,分享我的EnumHelper。

    在C#编程中玩转枚举,分享我的EnumHelper. 在软件开发过程中,我们经常会为特定的场景下的特定数据定义逻辑意义.比如在用户表中,我们可能会有一个用户状态字段,该字段为整形.如果该字段的值为1则 ...

  4. C# EnumHelper Enum的值,Description,ToString()的相互转换

    首先定义枚举类型,如下: /// <summary> /// 板块 /// </summary> public enum Plate {         [Descriptio ...

  5. EnumHelper.cs

    网上找的,还比较实用的: using System; using System.Collections.Generic; using System.ComponentModel; using Syst ...

  6. .net工具类

    ConvertHelper public class ConvertHelper { /// <summary> /// 转换类型 /// </summary> /// < ...

  7. c#枚举使用详解

    简介 1. 枚举(enum type)通常用来表示一组常量.由于枚举是强类型的,这在编程中给我们提供了极大的方便. 2. 枚举的定义: public enum Sex { 男 = 0, 女 = 1 } ...

  8. grape动态PHP结构(二)——管理后台

    一.概述

  9. 利用SCORE法则来总结一次偷懒的单元测试过程

    最近遇到一个单元测试的问题,本周正好学个了一个SCORE法则,这里正好练练手应用此法则将问题的前因后果分享给大家. S:背景  代码要有单元测试,检测的标准就是统计代码的单元测试覆盖率,程序员需要达到 ...

随机推荐

  1. bootstrap错误警告信息提示

    bootstrap提供了成功执行.警告和错误信息的样式. 在使用该功能的时候需要引入以下几个文件: bootstrap.css jquery.js(需放在bootstrap.js之前) bootstr ...

  2. Linq无聊练习系列1--where练习

    linq主要有3种,linq to sql,linq to XML,Linq to Object linq to sql. 这里没有通过相应的类,生成相应的数据库中的表.没有用流行的编码优先. 只是为 ...

  3. 基于多重信号分类算法的DOA估计

    原创博文,转载请注明出处 下面的论文是我的雷达处理的作业,拿来共享,不喜勿喷.由于公式编辑器的原因,无法复制公式,全部内容请点击. 基于多重信号分类算法的DOA估计 1引言 多重信号分类(MUSIC) ...

  4. Bootstrap3.0学习第六轮(表单)

    Bootstrap3.0学习第六轮(表单) 前言 阅读之前您也可以到Bootstrap3.0入门学习系列导航中进行查看http://www.cnblogs.com/aehyok/p/3404867.h ...

  5. [笔记] OS X and iOS 内核开发

    一.KEXT包的安全性说明 KEXT 程序包及其包含的所有文件及文件夹必须属于 root 用户(用户 id 是 0) KEXT 程序包及其包含的所有文件及文件夹必须属于 wheel 组(组 id 是 ...

  6. C语言变参函数的编写

    1. 引言 一般我们编程的时候,函数中形式参数的数目通常是确定的,在调用时要依次给出与形式参数对应的实际参数.但在某些情况下我 们希望函数的参数个数可以根据需要确定,因此c语言引入可变参数函数.典型的 ...

  7. (转)JS中公共/私有变量和方法

    私有变量 在对象内部使用'var'关键字来声明,而且它只能被私有函数和特权方法访问. 私有函数 在对象的构造函数里声明(或者是通过var functionName=function(){...}来定义 ...

  8. TCP/IP详解学习笔记- 概述

    TCP/IP详解学习笔记(1)-- 概述1.TCP/IP的分层结构      网络协议通常分不同层次进行开发,每一层分别负责不同的同信功能.TCP/IP通常被认为是一个四层协议系统.      如图所 ...

  9. 安全验证之使用摘要认证(digest authentication)

    安全验证之使用摘要认证(digest authentication) 在前一篇文章中,主要讨论了使用HTTP基本认证的方法,因为HTTP基本认证的方式决定了它在安全性方面存在很大的问题,所以接下来看看 ...

  10. C语言中判断int,long型等变量是否赋值的方法

    博主这段时间在写一些C程序的代码,由于以前对C不是了解很多,故遇到很多瓶颈,当然,其中也有很多有趣的方法可以利用以解决这些难题,下面这个问题就是博主遇到的一个麻烦. 声明了 int ,long 型等局 ...