获取枚举Description 属性
/// <summary>
/// 获取枚举变量值的 Description 属性
/// </summary>
/// <param name="obj">枚举变量</param>
/// <returns>如果包含 Description 属性,则返回 Description 属性的值,否则返回枚举变量值的名称</returns>
public static string GetDescription(this Enum obj)
{
string description = string.Empty;
try
{
Type _enumType = obj.GetType();
DescriptionAttribute dna = null;
FieldInfo fi = null;
var fields = _enumType.GetCustomAttributesData(); if (!fields.Where(i => i.Constructor.DeclaringType.Name == "FlagsAttribute").Any())
{
fi = _enumType.GetField(Enum.GetName(_enumType, obj));
dna = (DescriptionAttribute)Attribute.GetCustomAttribute(fi, typeof(DescriptionAttribute));
if (dna != null && string.IsNullOrEmpty(dna.Description) == false)
return dna.Description;
return null;
} GetEnumValuesFromFlagsEnum(obj).ToList().ForEach(i =>
{
fi = _enumType.GetField(Enum.GetName(_enumType, i));
dna = (DescriptionAttribute)Attribute.GetCustomAttribute(fi, typeof(DescriptionAttribute));
if (dna != null && string.IsNullOrEmpty(dna.Description) == false)
description += dna.Description + ",";
}); return description.EndsWith(",")
? description.Remove(description.LastIndexOf(','))
: description;
}
catch
{
return "未设定";
} }
/// <summary>
/// 得到Flags特性的枚举的集合
/// </summary>
/// <param name="value"></param>
/// <returns></returns>
static List<Enum> GetEnumValuesFromFlagsEnum(Enum value)
{
List<Enum> values = Enum.GetValues(value.GetType()).Cast<Enum>().ToList();
List<Enum> res = new List<Enum>();
foreach (var itemValue in values)
{
if (value.GetHashCode() >= itemValue.GetHashCode())//防止一些左而数小,后面数大的情况,严格规定左而有大数,右面为小数
if ((value.GetHashCode() & itemValue.GetHashCode()) > 0
|| (value.GetHashCode() == 0 && itemValue.GetHashCode() == 0))//输出为0的枚举元素
res.Add(itemValue);
}
return res;
}
获取枚举Description 属性的更多相关文章
- 获取枚举Description的Name
/// <summary> /// 获取枚举Description的Name /// </summary> /// <param name="enumName& ...
- 枚举扩展方法获取枚举Description
枚举扩展方法 /// <summary> /// 扩展方法,获得枚举的Description /// </summary> /// <param name="v ...
- c#获取枚举
在实际开发项目中,我们定义了一个枚举,往往我们需要在下拉框或其它地方展示枚举.为了加深印象,也为了帮到有需要的人,我写了一个DEMO. 第一步,我们定义一个枚举: /// <summary> ...
- C# Enum 获取枚举属性
Enum使用 获取枚举属性 注意:扩展方法必须定义为静态类,静态方法中. public enum EnumPatientSource { [Description("住院")] I ...
- C# 获取枚举的描述Description
方法类: public static class EnumExtensions { #region Enum /// <summary> /// 获取枚举变量值的 Description ...
- 【转载】[C#]枚举操作(从枚举中获取Description,根据Description获取枚举,将枚举转换为ArrayList)工具类
关键代码: using System; using System.Collections; using System.Collections.Generic; using System.Compone ...
- 获取枚举值上的Description特性说明
/// <summary> /// 获取枚举值上的Description特性说明 /// </summary> /// <typeparam name="T&q ...
- 获取枚举Name,Value,Description两种方法
using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.UI ...
- 根据枚举获取枚举的Description特性值
首先定义一个枚举:两个值:已确认.未确认. public enum ConfirmStatusEnum { [Description("未确认")] unconfirmed = , ...
随机推荐
- lr数据库参数化取数:The query result is empty and same is the parameter file问题原因
出现这个问题的原因: 是因为我们的查询结果存在中文 如果查询结果没有中文,显示正常 解决办法: 新建一个数据源: 重新再选择这个数据源,再次查询: 说明不是连接字符串的问题或者是mysql驱动的问题 ...
- HDU3729 I'm Telling the Truth(字典序最大的最大流)
题目大概说n个学生,都各自有一个互不相同的成绩排名,他们各自说了他们成绩排名所在区间,问最多有几个学生没说谎以及字典序最大的没说谎的学生序列. 学生作为一个X部的点,排名作为Y部的点,学生与其成绩排名 ...
- 【python游戏编程之旅】第四篇---pygame中加载位图与常用的数学函数。
本系列博客介绍以python+pygame库进行小游戏的开发.有写的不对之处还望各位海涵. 在上一篇博客中,我们学习了pygame事件与设备轮询.http://www.cnblogs.com/msxh ...
- 骨牌铺方格[HDU2046]
骨牌铺方格 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submi ...
- object-c [self class] 和 [self _cmd]
[self class] 返回当前类名 [self _cmd] 返回当前方法名 self 是类的隐藏的参数,指向当前当前调用方法的类 另一个隐藏参数是_cmd,代表当前类方法的selector
- TYVJ P1022 进制转换 Label:坑
背景 太原成成中学第3次模拟赛 第2道 描述 对于十进制整数N,试求其-2进制表示.例如,因为 1*1 + 1*-2 + 1*4 + 0*-8 +1*16 + 1*-32 = -13 ,所以(-13) ...
- 【POJ】1556 The Doors(计算几何基础+spfa)
http://poj.org/problem?id=1556 首先路径的每条线段一定是端点之间的连线.证明?这是个坑...反正我是随便画了一下图然后就写了.. 然后re是什么节奏?我记得我开够了啊.. ...
- TAT,我的LCT转双旋了
这里是rotate和splay函数 void rotate(int x) { ]==x; ch[y][d^]=ch[x][d];pre[ch[x][d]]=y; ch[z][ch[z][]==y]=x ...
- hdu A strange lift
有起点和终点,有方向,有最少次数,所以这道题很明显是一道bfs的题目,这题要利用vist数组来标记已走过的楼层,因为这题里面已走过的楼层是不可能在走第二遍的. 第二次走和第一次走的选择没有任何的区别. ...
- 修改CSV中的某些值 -- 1
修改前: col1,col2,col3,col4 text1,text2,text3,text4 text5,text6,text7,text8 text9,text10,text11,text12 ...