C#获取枚举的描述
public enum StatusEnum
{
/// <summary>
/// 运行中
/// </summary>
[Description("运行中")]
Running = ,
/// <summary>
/// 已关闭
/// </summary>
[Description("已关闭")]
Stopped =
}
public class EnumHelper
{
#region 静态方法
public static Dictionary<string, string> GetEnumDescription<T>()
{
Dictionary<string, string> dic = new Dictionary<string, string>(); FieldInfo[] fields = typeof(T).GetFields(); foreach (FieldInfo field in fields)
{ if (field.FieldType.IsEnum)
{ object[] attr = field.GetCustomAttributes(typeof(DescriptionAttribute), false); string description = attr.Length == ? field.Name : ((DescriptionAttribute)attr[]).Description; dic.Add(field.Name, description); }
} return dic;
} /// <summary>
/// 获取对应的枚举描述
/// </summary>
public static List<KeyValuePair<string, string>> GetEnumDescriptionList<T>()
{
List<KeyValuePair<string, string>> result = new List<KeyValuePair<string, string>>(); FieldInfo[] fields = typeof(T).GetFields(); foreach (FieldInfo field in fields)
{
if (field.FieldType.IsEnum)
{ object[] attr = field.GetCustomAttributes(typeof(DescriptionAttribute), false); string description = attr.Length == ? field.Name : ((DescriptionAttribute)attr[]).Description; result.Add(new KeyValuePair<string, string>(field.Name, description)); } }
return result;
} /// <summary>
/// 获取枚举的 值和描述
/// </summary>
/// <typeparam name="T"></typeparam>
/// <returns></returns>
public static List<KeyValuePair<int, string>> GetEnumValueDescriptionList<T>()
{
List<KeyValuePair<int, string>> result = new List<KeyValuePair<int, string>>();
FieldInfo[] fields = typeof(T).GetFields();
foreach (FieldInfo field in fields)
{
if (field.FieldType.IsEnum)
{
object[] attr = field.GetCustomAttributes(typeof(DescriptionAttribute), false);
string description = attr.Length == ? field.Name : ((DescriptionAttribute)attr[]).Description;
result.Add(new KeyValuePair<int, string>(Convert.ToInt32(field.GetValue(null)), description));
}
} return result;
} public static string GetDescriptionByEnumName<T>(string name)
{
try
{
Dictionary<string, string> dic = GetEnumDescription<T>();
string description = dic[name];
return description;
}
catch (Exception ex)
{
return "";
}
}
#endregion
}
C#获取枚举的描述的更多相关文章
- .NET获取枚举DescriptionAttribute描述信息性能改进的多种方法
一. DescriptionAttribute的普通使用方式 1.1 使用示例 DescriptionAttribute特性可以用到很多地方,比较常见的就是枚举,通过获取枚举上定义的描述信息在UI上显 ...
- 枚举Enum转换为List,获取枚举的描述
代码: public class EnumberHelper { public static List<EnumberEntity> EnumToList<T>() { Lis ...
- C# 获取枚举的描述属性
在使用枚举类型时,我们需要取名称和值,甚至有时候还需要取枚举类型的描述.通过反射,我们能获取到枚举类型的描述属性. 首先我们需要给枚举类型添加描述属性(属性都没有是不可能取到的),[Descripti ...
- C# 获取枚举的描述Description
方法类: public static class EnumExtensions { #region Enum /// <summary> /// 获取枚举变量值的 Description ...
- C#获取枚举描述代码
public class MusterEnum { /// 获取枚举的描述信息 /// </summary> /// <param name="e">传入枚 ...
- c#枚举 获取枚举键值对、描述等
using System; using System.Collections.Generic; using System.Collections.Specialized; using System.C ...
- .net工具类 获取枚举类型的描述
一般情况我们会用枚举类型来存储一些状态信息,而这些信息有时候需要在前端展示,所以需要展示中文注释描述. 为了方便获取这些信息,就封装了一个枚举扩展类. /// <summary> /// ...
- C#记录日志、获取枚举值 等通用函数列表
) { ] >= && ipvals[] <= && ipval ...
- c#枚举的描述和值
using System; using System.Collections.Generic; using System.ComponentModel; using System.Linq; usin ...
随机推荐
- 在window的IIS中搭配Php的简单方法
在window的IIS中搭配Php的简单方法.搭配php的时候找到的一个超级简单方法 关键的核心是 PHP Manager for IIS 这是微软开发的一个项目,使用它可以在window下最方便简单 ...
- uedit富文本编辑器及图片上传控件
微力后台 uedit富文本编辑器及文件上传控件的使用,无时间整理,暂略,参考本地代码.能跑起来.
- angularjs之$ajax请求
AngularJS不仅仅只有双向绑定等等功能,还有发送Ajax请求的Api. 效果图: 请求的文件(data.php): <?php $data = [ '股市下跌', '清明小长假结束', ' ...
- python 2.7中安装mysql
在python中进行安装mysql模块,但是怎么都不能导入mysql模块,出错如下所示: [root@python ~]# python Python 2.7.11 (default, Apr 5 2 ...
- javascript 特定字符分隔字符串函数
function fn(num,div,token){//num需要分割的数字,div多少位分割 token分割字符 num=num+'',div=div||3,token=token||',' re ...
- TNS:no listener error in Oracle XE after changing computer name
This morning at work when trying to log on to my computer I noticed not my username on login screen ...
- 1 数据库开发--MySQL介绍
1.数据库管理软件 C/S 并发.锁 :SQL语句.语法 基本管理和语法学习 一.介绍: mysql数据库管理软件: 套接字:服务端,客户端:客户端可访问服务端得数据 1.支持并发:操作得是共享得数据 ...
- FP扣损耗逻辑代码
芯片172没有扣减损耗, 取数:SAP_STPO的AUSCH IN_BOM_DETAILS:耗损比例COMPONENT_YIELD_UOM 存储过程FP_MO2SAP:supplydmdpeg ...
- gorm中自己写sql的方法实现
type Result struct { Total int } var result Result //当天修改作业的总时间:分钟 dao.DB(dao.HomeworkTable).Raw(&qu ...
- LinuxC编程怎么MakeFile
在linux下我们都知道可以利用命令gcc hello.c -o hello 命令来变异c语言程序.其中gcc hello.c -o hello中 hello是给这个编译后生成的可执行文件取个别名 再 ...