获取枚举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 = , ...
随机推荐
- HTML-Canvas03
颜色合成 globalCompositeOperation 属性: //先绘制一个图形. ctx.fillStyle = "#00ff00"; ctx.fillRect(10,10 ...
- Kali Linux 2016.2发布提供虚拟机以及系统镜像下载
Kali Linux 2016.2发布提供虚拟机以及系统镜像下载 Kali Linux 2016.2发布提供虚拟机以及系统镜像下载,本次Kali Linux 2016.2提供了五种桌面模式,分别为 ...
- 打开别人Xamarin项目找不到android.jar文件
打开别人Xamarin项目找不到android.jar文件 错误信息:Could not find android.jar for API Level 23.打开非本机创建的Xamarin项目,编译的 ...
- push和join
Push,向数组末尾添加元素,并返回长度. Join,将数组按照join参数连接起来. 不同浏览器对JS解析不同,Join比连接字符串快,要用大量数据测试,然后比时间.
- 10692 XYM-入门之道
Description 在华农的ACM界中,也有一对闻名古今的双胖师徒组合—XYM和BM. BM师父有一个特殊的癖好,BM肚子很大,因为他很 喜欢吃西瓜,但是BM的嘴很小,一次只能吃下大小不超过K的西 ...
- LIS HDOJ 1257 最少拦截系统
题目传送门 题意:中文题面 分析:LIS模板题:n - 最长下降子序列 -> 最长上升子序列 贪心做法以后再补:) 代码: #include <cstdio> #include &l ...
- 初始化lpc2106开发工程
单片机型号:lpc2106.Init.s:初始化pc指针和sp指针. AREA Init, CODE, READONLY IMPORT test1_main EXPORT ...
- HDU 1561 (树形DP+背包)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1561 题目大意:从树根开始取点.最多取m个点,问最大价值. 解题思路: cost=1的树形背包. 有 ...
- C++ Split string into vector<string> by space
在C++中,我们有时候需要拆分字符串,比如字符串string str = "dog cat cat dog"想以空格区分拆成四个单词,Java中实在太方便了,直接String[] ...
- mysql主从配置脚本
PASSWD=123456USER=rootREP_HOST=10.10.10.70REP_PORT=3306REP_USER=slaveREP_PASSWD=123456@REP_FILE=mysq ...