EnumHelper.cs
网上找的,还比较实用的:
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的更多相关文章
- EnumHelper.cs枚举助手(枚举描述信息多语言支持)C#
C#里面经常会用到枚举类型,枚举是值类型对象,如果你想用枚举类型的多属性特性,或者你想在MVC页面上通过简单的值类型转换,将某字段值所代表的含义转换为文字显示,这时候必须要将枚举扩展,是它支持文本描述 ...
- 特性Atrribute和枚举
特性的简单实用!特性存放在metedata里面,它离不开反射. Program.cs class Program { static void Main(string[] args) { Console ...
- “PMS-基础权限管理系统”实施某谱OA系统经验总结
“PMS-基础权限管理系统”介绍 "PMS-基础权限管理系统"是我一直想做的一个产品,融合多年开发及维护管理系统的经验,参考了很多系统,精心研制而成. 可以做为毕业设计参考,新手学 ...
- .NET MVC EF框架数据库连接配置
1:数据库的配置和连接 Web.config <connectionStrings> <add name="SQLConnectionString" connec ...
- ASP.NET MVC 5.0 参考源码索引
http://www.projky.com/asp.netmvc/5.0/Microsoft/AspNet/Mvc/Facebook/FacebookAppSettingKeys.cs.htmlhtt ...
- [C#] 剖析 AssemblyInfo.cs - 了解常用的特性 Attribute
剖析 AssemblyInfo.cs - 了解常用的特性 Attribute [博主]反骨仔 [原文]http://www.cnblogs.com/liqingwen/p/5944391.html 序 ...
- Atitit 软件架构方法的进化与演进cs bs soa roa msa attilax总结
Atitit 软件架构方法的进化与演进cs bs soa roa msa attilax总结 1.1. 软件体系架构是沿着单机到 CS 架构,再到 BS 的三层架构甚至多层架构逐步发展过来的,关于 ...
- 从java文件和CS文件里查询方法使用次数工具
前几天,领导让我找一下老系统(Java)里getRemoteUser方法都哪个文件用了,package是什么,方法被调用了多少次,当时因为着急,所以,直接人工找的,但是以后要是再出现,人工找就太讨厌了 ...
- 关于 WP 开发中.xaml 与.xaml.cs 的关系
今天我们先来看一下在WP8.1开发中最长见到的几个文件之间的关系.比较论证,在看这个问题之前我们简单看看.NET平台其他两个不同的框架: Windows Forms 先看看Window Forms中的 ...
随机推荐
- 剑指offer19:按照从外向里以顺时针的顺序依次打印出每一个数字,4 X 4矩阵: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 则依次打印出数字1,2,3,4,8,12,16,15,14,13,9,5,6,7,11,10.
1 题目描述 输入一个矩阵,按照从外向里以顺时针的顺序依次打印出每一个数字,例如,如果输入如下4 X 4矩阵: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 则依次打印 ...
- MySQL 设置密码和允许远程登录
mysqladmin -u root password "newpass" GRANT ALL PRIVILEGES ON *.* TO root' WITH GRANT OPTI ...
- JSON在JS中的应用
一. JSON在JS中的应用: 首先解释下JSON对象与普通js对象字面量定义时格式的区别: Js对象字面量定义格式: var person = { name:"Wede", ag ...
- luogu4777[模板]拓展中国剩余定理题解
题目链接 https://www.luogu.org/problemnew/show/P4777 分析 扩展\(CRT\)就是解决模数不互质的情况,说是扩展\(CRT\),其实都是扩欧... 先来考虑 ...
- Linux更新程序脚本
DATE=$(date +%Y%m%d_%H%M%S) cd /opt/anystreaming/transcoder/ mv dll/libmonitor_service.so "dll/ ...
- .net core默认不支持gb2312
采集数据时,乱码,之前遇到过这个情况,于是老办法: 果断使用Encoding.GetEncoding(“GB2312”),抛异常.搜了下,是因为.net core默认不支持gb2312 所以,两个办法 ...
- 【php设计模式】策略模式
策略模式是针对一组算法,将每一种算法都封装到具有共同接口的独立的类中,从而是它们可以相互替换.策略模式的最大特点是使得算法可以在不影响客户端的情况下发生变化,从而改变不同的功能. <?php i ...
- wireshark 分析过滤数据
1.过滤IP,如来源IP或者目标IP等于某个IP例子:ip.src eq 192.168.1.107 or ip.dst eq 192.168.1.107或者ip.addr eq 192.168.1. ...
- SQL学习——IN运算符
IN的作用 IN运算符允许您在WHERE子句中指定多个值. IN运算符是多个OR条件的简写. IN的语法 SELECT column_name(s) FROM table_name WHERE col ...
- 10.Spring整合Hibernate_3_HibernateTemplate
将sessionFactory 注入给 hibernateTemplate,让hibernateTemplate帮我们完成一些模板代码 <!-- 使用HibernateTemplate --&g ...