/// <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 属性的更多相关文章

  1. 获取枚举Description的Name

    /// <summary> /// 获取枚举Description的Name /// </summary> /// <param name="enumName& ...

  2. 枚举扩展方法获取枚举Description

    枚举扩展方法 /// <summary> /// 扩展方法,获得枚举的Description /// </summary> /// <param name="v ...

  3. c#获取枚举

    在实际开发项目中,我们定义了一个枚举,往往我们需要在下拉框或其它地方展示枚举.为了加深印象,也为了帮到有需要的人,我写了一个DEMO. 第一步,我们定义一个枚举: /// <summary> ...

  4. C# Enum 获取枚举属性

    Enum使用 获取枚举属性 注意:扩展方法必须定义为静态类,静态方法中. public enum EnumPatientSource { [Description("住院")] I ...

  5. C# 获取枚举的描述Description

    方法类: public static class EnumExtensions { #region Enum /// <summary> /// 获取枚举变量值的 Description ...

  6. 【转载】[C#]枚举操作(从枚举中获取Description,根据Description获取枚举,将枚举转换为ArrayList)工具类

    关键代码: using System; using System.Collections; using System.Collections.Generic; using System.Compone ...

  7. 获取枚举值上的Description特性说明

    /// <summary> /// 获取枚举值上的Description特性说明 /// </summary> /// <typeparam name="T&q ...

  8. 获取枚举Name,Value,Description两种方法

    using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.UI ...

  9. 根据枚举获取枚举的Description特性值

    首先定义一个枚举:两个值:已确认.未确认. public enum ConfirmStatusEnum { [Description("未确认")] unconfirmed = , ...

随机推荐

  1. Android Bitmap详细介绍

    package com.testbitmapscale; import java.io.File; import java.io.FileInputStream; import java.io.Fil ...

  2. mysql一次插入多条数据

    mysql一次插入多条数据: INSERT INTO hk_test(username, passwd) VALUES ('qmf2', 'qmf2'),('qmf3', 'qmf3'),('qmf4 ...

  3. D6 I

    I - I Time Limit:1000MS     Memory Limit:2048KB     64bit IO Format:%lld & %llu Submit Status Pr ...

  4. 递推DP URAL 1017 Staircases

    题目传送门 /* 题意:给n块砖头,问能组成多少个楼梯,楼梯至少两层,且每层至少一块砖头,层与层之间数目不能相等! 递推DP:dp[i][j] 表示总共i块砖头,最后一列的砖头数是j块的方案数 状态转 ...

  5. ZOJ 3905 Cake ZOJ Monthly, October 2015 - C

    Cake Time Limit: 4 Seconds      Memory Limit: 65536 KB Alice and Bob like eating cake very much. One ...

  6. HDU 5212 Code

    筛法. 统计所有 [数] 的所有 [倍数] 的 [数] 的个数,即 i 的所有倍数 i, 2i, 3i, 4i...个数为 dp[i], 则所有 倍数两两结合共有 dp[i] * dp[i] 个. 此 ...

  7. js html5推送 实例

    <!DOCTYPE html>   <html>   <head>   <title>Simple Webkit notification exampl ...

  8. CentOS6.4 安装Nagios 并监控端口

    1.下载所需文件nagios-3.4.3.tar.gz,nagios-plugins-1.4.15.tar.gz,nrpe-2.14.tar.gz,sendEmail-v1.56.tar.gz 下载地 ...

  9. Flex 4中组件背景设置(填充方式)group为例子

    以下以Group为例子讲述如何在Flex 4中填充背景颜色.图片: 1.图片填充方式: <s:Group x="0" y="0" height=" ...

  10. Video Codecs by FOURCC 视频格式编码

    FOURCC Name Summary 1978 A.M.Paredes predictor This is a LossLess video codec. >>> 2VUY 2VU ...