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. svn外网访登录不进去提示证书错误Authorization Required

    为了外网能访问内网svn.于是坐在外网端口映射.但是奇怪的是内网能访问,外网总也登录不进去.以为是浏览器版本低 但是其他浏览器也一样.最后客户端也登录不进去.提示报错:  Authorization  ...

  2. asp.net mvc放在iis7.5中提示404错误 js异步请求失效解决办法

    asp.net mvc中js发请求一般写成: $.get("/Can/index"本地上是没有问题的但是部署到iis上,提示404,正确的请求的路径是:/网站名/Can/index ...

  3. idea执行go

    因为经常在不同的地方调代码,每次都调整环境很麻烦,于是在犯懒的时候发现了更直接简便的办法,关于idea集成go环境的,不需要按部就班的部署. 首先下代码,比如https://github.com/sa ...

  4. Manager(管理器)

    Manager(管理器) 索引 意图 结构 参与者 适用性 效果 实现 实现方式(一):Manager 模式的示例实现. 意图 将对一个类的所有对象的管理封装到一个单独的管理器类中. 这使得管理职责的 ...

  5. WXPP QuickFramework V2.0

    微信快速开发框架(WXPP QuickFramework)V2.0版本上线--源码已更新至github   用了一个多星期的时间,把微信快速开发框架进行了改进,之前1.0版本针对的是普通订阅号,V2. ...

  6. .NET代码自动编译发布

    .NET代码自动编译发布   因本人一直使用.NET开发,在做项目的时候,每次都要涉及到各个环境的部署问题,手工操作容易出错,并且重复劳动多,所以一直在寻找一个能实现自动化部署的方案. 废话不多讲,先 ...

  7. 用Python复习离散数学(一)

    最近要复习离散数学,不想挂啊,但是又想编程,大家知道啦,程序员离不开代码啊,所用想边复习边写代码,所以就自己用代码去实现一下离散的知识点,当做复习,自知自己的Python很渣,也想借此巩固一下基础,哈 ...

  8. 【IOS开发】搜索和排序(好友列表,通讯录的实现,searchbar)

    一.效果图: 二.概述 实现一个好友列表,可以分为男女两个选项,并且实现搜索和排序功能.我的数据是放在plist文件中. 三.代码简述 代码结构如图,首先自定义一个Cell. cell.h #impo ...

  9. 写JQuery 插件

    什么?你还不会写JQuery 插件 前言 如今做web开发,jquery 几乎是必不可少的,就连vs神器在2010版本开始将Jquery 及ui 内置web项目里了.至于使用jquery好处这里就不再 ...

  10. C#山寨版本拨号客户端

    C#山寨版本[天翼拨号客户端]---内含详细抓包,模拟数据---万事俱备,只欠东风.   本帖子本来最初是发在CSDN上的,回复的也有十几个,但没有一个有技术含量的回复....特来此讨论,请教,请各位 ...