/// <summary>
/// get enum description by name
/// </summary>
/// <typeparam name="T">enum type</typeparam>
/// <param name="enumItemName">the enum name</param>
/// <returns></returns>
public static string GetDescriptionByName<T>(T enumItemName)
{
FieldInfo fi = enumItemName.GetType().GetField(enumItemName.ToString()); DescriptionAttribute[] attributes = (DescriptionAttribute[])fi.GetCustomAttributes(
typeof(DescriptionAttribute), false); if (attributes != null && attributes.Length > )
{
return attributes[].Description;
}
else
{
return enumItemName.ToString();
}
}
         /// <summary>
/// 获取描述
/// </summary>
/// <param name="en"></param>
/// <returns></returns>
public static string GetEnumText(this Enum en)
{
FieldInfo fi = en.GetType().GetField(en.ToString()); DescriptionAttribute[] attributes = (DescriptionAttribute[])fi.GetCustomAttributes(
typeof(DescriptionAttribute), false); if (attributes != null && attributes.Length > )
{
return attributes[].Description;
}
else
{
return en.ToString();
}
}
         /// <summary>
/// 枚举转list
/// </summary>
/// <typeparam name="T"></typeparam>
/// <returns></returns>
public static List<EnumModel> ConvertEnumToList<T>()
{
List<EnumModel> list = new List<EnumModel>(); foreach (var e in Enum.GetValues(typeof(T)))
{
EnumModel m = new EnumModel();
m.Value = Convert.ToInt32(e);
m.Description = EnumHelper.GetDescriptionByName((T)e);
list.Add(m);
}
return list;
} public class EnumModel
{
public int Value { get; set; }
public string Description { get; set; }
}
         /// <summary>
/// 根据枚举值获得枚举描述
/// </summary>
/// <param name="e"></param>
/// <param name="value"></param>
/// <returns></returns>
public static String GetEnumDesc(this Type e, int? value)
{
FieldInfo[] fields = e.GetFields();
for (int i = , count = fields.Length; i < count; i++)
{
if ((int)System.Enum.Parse(e, fields[i].Name) == value)
{
DescriptionAttribute[] EnumAttributes = (DescriptionAttribute[])fields[i].
GetCustomAttributes(typeof(DescriptionAttribute), false);
if (EnumAttributes.Length > )
{
return EnumAttributes[].Description;
}
}
}
return "";
}

.NET--------枚举扩展方法(枚举转list,获取枚举描述)的更多相关文章

  1. C#枚举扩展方法,获取枚举值的描述值以及获取一个枚举类下面所有的元素

    /// <summary> /// 枚举扩展方法 /// </summary> public static class EnumExtension { private stat ...

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

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

  3. .NET(C#):使用反射来获取枚举的名称、值和特性【转】

    首先需要从内部了解一下枚举(Enumeration),相信许多人已经知道了,当我们声明一个这样的枚举类型: enumMyEnum { AAA, BBB, CCC } 背后的IL是这样的: .class ...

  4. .NET(C#):使用反射来获取枚举的名称、值和特性

    首先需要从内部了解一下枚举(Enumeration),相信许多人已经知道了,当我们声明一个这样的枚举类型: enum MyEnum { AAA, BBB, CCC } 背后的IL是这样的: .clas ...

  5. c#获取枚举

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

  6. jsp获取枚举的值

    Struts2的Action传回页面一个list,页面迭代这个list,获取下拉框的值,获取过来是枚举类型. 在jsp页面获取枚举的常量值和枚举的值的例子如下: jsp页面: <td >状 ...

  7. C#的扩展方法解析

    在使用面向对象的语言进行项目开发的过程中,较多的会使用到“继承”的特性,但是并非所有的场景都适合使用“继承”特性,在设计模式的一些基本原则中也有较多的提到. 继承的有关特性的使用所带来的问题:对象的继 ...

  8. C# 知识回顾 - 扩展方法解析

    在使用面向对象的语言进行项目开发的过程中,较多的会使用到“继承”的特性,但是并非所有的场景都适合使用“继承”特性,在设计模式的一些基本原则中也有较多的提到. 继承的有关特性的使用所带来的问题:对象的继 ...

  9. Farseer.net轻量级开源框架 中级篇:常用的扩展方法

    导航 目   录:Farseer.net轻量级开源框架 目录 上一篇:Farseer.net轻量级开源框架 中级篇: BasePage.BaseController.BaseHandler.BaseM ...

  10. WebAPi添加常用扩展方法及思维发散

    前言 在WebAPi中我们通常需要得到请求信息中的查询字符串或者请求头中数据再或者是Cookie中的数据,如果需要大量获取,此时我们应该想到封装一个扩展类来添加扩展方法,从而实现简便快捷的获取. We ...

随机推荐

  1. 微信小程序点击列表添加 去除属性

    首先说一下场景:我所循环的数据是对象数组,设置了一个属性当作标记,通过这个标记的值判断是否给改元素添加样式 wxml: <view> <view wx:for="{{lis ...

  2. Lombok之使用详解

    前言 在Java中,封装是一个非常好的机制,最常见的封装莫过于get,set方法了,无论是Intellij idea 还是Eclipse,都提供了快速生成get,set方法的快捷键,使用起来很是方便, ...

  3. 2 - Binary Search & LogN Algorithm

    254. Drop Eggs https://www.lintcode.com/problem/drop-eggs/description?_from=ladder&&fromId=1 ...

  4. linux软件管理之python包管理

    Python包管理 ====================================================================================python ...

  5. LVM (逻辑卷管理器)

    图片来自:https://www.cnblogs.com/linuxprobe/p/5381538.html 参考博客:https://www.cnblogs.com/linuxprobe/p/538 ...

  6. h5页面避免两个页面反复跳转死循环

    项目中经常会碰到在一个页面加判断之后跳转另一个页面,并且无法返回,来回跳转,死循环,遇到这种情况可以在跳转时使用location,replace,关闭之前页面,与location.href 两者的不同 ...

  7. MySQL安装脚本0104-亲试ok

    #!/bin/bash export host_ip=192.168.7.79 export password=123456   echo '#1.取master主机ip的后三位作为master的se ...

  8. linux-基础命令篇-01

    基本命令: who:目前有谁在在线 netstat -a:网络的联机状态 ps -aux:背景运行的程序 sync:将数据同步写入硬盘中的命令 shutdown:惯用的关机命令 reboot, hal ...

  9. Tensorflow选择性初始化图中的变量

    import tensorflow as tf def initialize_uninitialized(sess): global_vars = tf.global_variables() is_n ...

  10. 【C/C++】数组 & 指针

    int main() { ]; ]; ][]; ]; ]; ]; ][]; cout << sizeof(a) << endl; cout << sizeof(pa ...