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中的 ...
随机推荐
- Photon Server 实现注册与登录(一) --- Hibernate整合到项目中
本系列实现目的:基于Photon Server实现注册于登录 一.拷贝Nbibernate项目的文件到MyGamerServer项目中. 二.数据库新建表,结构如下 三.修改文件名和配置 (1).将拷 ...
- Django新手入门必看
pip install django==2.1.7 (现在Django3.0出来,推荐大家可以使用一下Django3.0) pip list查看
- AtCoder Beginner Contest 144 题解
传送门 $cf$ 自闭了,打 $abc$ 散散心 A - 9x9 ...这个有什么好讲的吗,题目看懂就会做了 #include<iostream> #include<cstdio&g ...
- Go的包管理工具(一)
在前面转载了系列文章:Golang 需要避免踩的 50 个坑,总得来说阅读量都挺大.今天这篇文章,咱们一起聊聊Go的依赖包管理工具. 背景 每一门语言都有其依赖的生态,当我们使用Java语言的时候,使 ...
- 基于APM实现RPC服务和消息队列的指定消费
本文内容是基于公司现有框架整理的一篇专利文章.该框架包含完整的一套DevOps流程,包括工单系统(容器申请.服务部署等)\配置中心\路由配置中心\服务治理平台\消息治理平台\葛朗台(基于Docker+ ...
- .Net下二进制形式的文件存储与读取
.Net下图片的常见存储与读取凡是有以下几种:存储图片:以二进制的形式存储图片时,要把数据库中的字段设置为Image数据类型(SQL Server),存储的数据是Byte[].1.参数是图片路径:返回 ...
- HTML练习一
效果图 动态图 html代码 <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> ...
- MySQL自测测试
#建学生信息表student create table student ( sno varchar(20) not null primary key, sname varchar(20) not nu ...
- jvm常用命令
jps // 查看Java进程ID和main方法类名 jstack <进程ID> // 查看该进程的所有栈信息 jstack -l <进程ID> // 查看该进程的所有栈信息, ...
- Stanford NLP 课程笔记之计算字符串距离
在自然语言处理任务中,有时候需要计算两个字符串之间的相似度,也可以称作是两者之间的距离,用最小编辑距离表示. 最小编辑距离用{Insertion,Deletion,Substitution}这三种操作 ...