获取枚举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 = , ...
随机推荐
- 克隆或拷贝的VMware虚拟机IP问题解决
克隆的虚拟机或是将虚拟机通过快照回到过去某个状态后,会上不了网. 如果不想看细节,可直接跳到小结部分. 问题描述: 运行service network restart时 Device eth0 doe ...
- Oracle TNS配置浅析
1. 什么是TNS? TNS是Oracle Net的一部分,专门用来管理和配置Oracle数据库和客户端连接的一个工具,在大多数情况下客户端和数据库要通讯,必须配置TNS,当然在少数情况下,不用配置T ...
- OGRE: "OgreOverlaySystem.h": No such file or directory
这两天学习OGRE,遇到"OgreOverlaySystem.h": No such file or directory的错误. 这是由于OGRE提供的例子过老,和SDK版本不一致 ...
- C#中DataTable使用技巧
在项目中经常用到DataTable,如果DataTable使用得当,不仅能使程序简洁实用,而且能够提高性能,达到事半功倍的效果,现对DataTable的使用技巧进行一下总结. 一.DataTable简 ...
- 洛谷 P1541 乌龟棋 Label:O(n^4)的dp
题目背景 小明过生日的时候,爸爸送给他一副乌龟棋当作礼物. 题目描述 乌龟棋的棋盘是一行N个格子,每个格子上一个分数(非负整数).棋盘第1格是唯一的起点,第N格是终点,游戏要求玩家控制一个乌龟棋子从起 ...
- 【wikioi】1002 搭桥(dfs+最小生成树)
http://wikioi.com/problem/1002/ 今天开始又开始刷水了哈T_T.照着hzwer神犇的刷题记录刷!!! 题解: 一开始我也不会,但是我想到了直接爆搜T_T. 好吧,题解. ...
- 一个简单的SqlServer游标使用
declare @id int ) declare c_department cursor for select id,name from department open c_department f ...
- Ubuntu 循环遍历当前目录下所有文本文件中的字符
sudo grep -n 'xxxx' -r ./*
- hdu Inverting Cups
这题需要分类讨论: 第一种情况: n为奇数m为偶数的情况无解,因为m为偶数,每次翻转将把从正面翻到反面的个数x减去从反面翻到正面的个数y,得到的数必定为偶数.因为x+y为偶数,x-y也为偶数.而总个数 ...
- POJ 1144 Network(Tarjan求割点)
Network Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 12707 Accepted: 5835 Descript ...