网上找的,还比较实用的:

 using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks; namespace Dapper.Tool
{
/// <summary>
/// 枚举扩展方法类
/// </summary>
public class EnumHelper
{
/// <summary>
/// 返回枚举值的描述信息。
/// </summary>
/// <param name="value">要获取描述信息的枚举值。</param>
/// <returns>枚举值的描述信息。</returns>
public static string GetEnumDesc<T>(object 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 (!string.IsNullOrEmpty(attr?.Description))
return attr.Description; 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 (!string.IsNullOrEmpty(attr?.Description))
return attr.Description; 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 (!string.IsNullOrEmpty(attr?.Description))
{
// 跳过过滤项
if (Array.IndexOf<string>(filterItem, attr.Description) != -)
{
continue;
}
// 添加
EnumKeyValue model = new EnumKeyValue
{
Key = (int) Enum.Parse(typeof (T), item.Name),
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; }
}
}
}

EnumHelper.cs的更多相关文章

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

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

  2. 特性Atrribute和枚举

    特性的简单实用!特性存放在metedata里面,它离不开反射. Program.cs class Program { static void Main(string[] args) { Console ...

  3. “PMS-基础权限管理系统”实施某谱OA系统经验总结

    “PMS-基础权限管理系统”介绍 "PMS-基础权限管理系统"是我一直想做的一个产品,融合多年开发及维护管理系统的经验,参考了很多系统,精心研制而成. 可以做为毕业设计参考,新手学 ...

  4. .NET MVC EF框架数据库连接配置

    1:数据库的配置和连接 Web.config <connectionStrings> <add name="SQLConnectionString" connec ...

  5. ASP.NET MVC 5.0 参考源码索引

    http://www.projky.com/asp.netmvc/5.0/Microsoft/AspNet/Mvc/Facebook/FacebookAppSettingKeys.cs.htmlhtt ...

  6. [C#] 剖析 AssemblyInfo.cs - 了解常用的特性 Attribute

    剖析 AssemblyInfo.cs - 了解常用的特性 Attribute [博主]反骨仔 [原文]http://www.cnblogs.com/liqingwen/p/5944391.html 序 ...

  7. Atitit 软件架构方法的进化与演进cs bs soa roa  msa  attilax总结

    Atitit 软件架构方法的进化与演进cs bs soa roa  msa  attilax总结 1.1. 软件体系架构是沿着单机到 CS 架构,再到 BS 的三层架构甚至多层架构逐步发展过来的,关于 ...

  8. 从java文件和CS文件里查询方法使用次数工具

    前几天,领导让我找一下老系统(Java)里getRemoteUser方法都哪个文件用了,package是什么,方法被调用了多少次,当时因为着急,所以,直接人工找的,但是以后要是再出现,人工找就太讨厌了 ...

  9. 关于 WP 开发中.xaml 与.xaml.cs 的关系

    今天我们先来看一下在WP8.1开发中最长见到的几个文件之间的关系.比较论证,在看这个问题之前我们简单看看.NET平台其他两个不同的框架: Windows Forms 先看看Window Forms中的 ...

随机推荐

  1. dedecms发布文章时间显示多少分钟前

    /**文章发布多少时间前*/function tranTime($time) { $rtime = date("m-d H:i",$time); $htime = date(&qu ...

  2. 20190804-Python基础 第二章 列表和元组

    容器,Python支持一种数据结构的基本概念(容器,基本上就是可包含其他对象的对象.) 两种主要的容器是:序列(如列表和元组)和映射(如字典) Ps: 列表与元组区别,列表可修改,元组不能. 对序列的 ...

  3. Rikka with Competition hdu 6095

    签到题目,排序然后按序清理掉一定会输的结果就可以. ac代码: #include <iostream> #include <cstdio> #include <cstri ...

  4. eventFlow 系列 <一> 入门

    var exampleId = ExampleId.New; var commandBus = resolver.Resolve<ICommandBus>(); ,) var execut ...

  5. VBA学习资料分享-2

    想利用VBA自动创建/发送OUTLOOK邮件,可以借助MailItem的Body属性或HTMLBody属性,代码模板如下: Dim objOutlook As Outlook.Application ...

  6. npm install 报错踩坑路

    先出现的是超过最大调用栈问题: npm ERR! Maximum call stack size exceeded 百度之后说给npm降级或者升级 降级 : npm install -g npm@5. ...

  7. 转载一篇让你全面了解什么是.NET。

    转载于:https://www.52abp.com/BlogDetails/10009 阅读文本大概需要 8 分钟. 持续进化的 .NET 这张图即是一个学习的路线图同样他也是 .NET 平台的进化图 ...

  8. vue项目中图片预览旋转功能

    最近项目中需要在图片预览时,可以旋转图片预览,在网上找了下,发现有一款功能强大的图片组件:viewerjs. git-hup: https://github.com/fengyuanchen/view ...

  9. liunx加载JX2410标准配置文件

  10. Nagios4.x安装配置总结

    1.  Nagios介绍 Nagios是一个监视系统运行状态和网络信息的监视系统.Nagios能监视所指定的本地或远程主机以及服务,同时提供异常通知功能等. Nagios可运行在Linux/Unix平 ...