C#获取枚举描述代码
public class MusterEnum
{
/// 获取枚举的描述信息
/// </summary>
/// <param name="e">传入枚举对象</param>
/// <returns>得到对应描述信息</returns>
public String GetEnumDesc(Enum e)
{
FieldInfo EnumInfo = e.GetType().GetField(e.ToString());
if (EnumInfo == null)
{
return "";
}
DescriptionAttribute[] EnumAttributes
= (DescriptionAttribute[])EnumInfo.GetCustomAttributes(typeof(DescriptionAttribute), false);
if (EnumAttributes.Length > 0)
{
return EnumAttributes[0].Description;
}
return e.ToString();
} /// <summary>
/// 将含有描述信息的枚举绑定到列表控件中
/// </summary>
/// <param name="listControl"></param>
/// <param name="enumType"></param>
private Dictionary<string, string> BindDesEnumToListControl(System.Type enumType)
{
Dictionary<string, string> dic = new Dictionary<string, string>();
foreach (object enumValue in Enum.GetValues(enumType))
{
Enum e = (Enum)enumValue;
dic.Add(((int)enumValue).ToString(), GetEnumDesc(e));
}
return dic;
}
}

还有一个是根据传入的枚举的数字索引直接获取

public static string GetEnumDesc<T>(Object obj)
{
obj = (T)obj;
if (obj == null) throw new ArgumentNullException("参数不能为null");
if (!obj.GetType().IsEnum) throw new Exception("参数类型不正确");
FieldInfo fieldinfo = obj.GetType().GetField(obj.ToString()); string str = string.Empty;
Object[] objs = fieldinfo.GetCustomAttributes(typeof(DescriptionAttribute), false);
if (objs != null && objs.Length != 0)
{
DescriptionAttribute des = (DescriptionAttribute)objs[0];
str = des.Description;
}
return str;
}
C#获取枚举描述代码的更多相关文章
- .NET--------枚举扩展方法(枚举转list,获取枚举描述)
/// <summary> /// get enum description by name /// </summary> /// <typeparam name=&qu ...
- .NET获取枚举DescriptionAttribute描述信息性能改进的多种方法
一. DescriptionAttribute的普通使用方式 1.1 使用示例 DescriptionAttribute特性可以用到很多地方,比较常见的就是枚举,通过获取枚举上定义的描述信息在UI上显 ...
- C#枚举扩展方法,获取枚举值的描述值以及获取一个枚举类下面所有的元素
/// <summary> /// 枚举扩展方法 /// </summary> public static class EnumExtension { private stat ...
- 【转载】[C#]枚举操作(从枚举中获取Description,根据Description获取枚举,将枚举转换为ArrayList)工具类
关键代码: using System; using System.Collections; using System.Collections.Generic; using System.Compone ...
- c#获取枚举
在实际开发项目中,我们定义了一个枚举,往往我们需要在下拉框或其它地方展示枚举.为了加深印象,也为了帮到有需要的人,我写了一个DEMO. 第一步,我们定义一个枚举: /// <summary> ...
- C#通过反射进行枚举描述相关操作
C#可以通过反射,来获取枚举的描述信息或通过描述信息获取到指定类型的枚举 /// <summary> /// 获取枚举描述 /// </summary> /// <par ...
- C# 读取枚举描述信息实例
using System;using System.Collections;using System.Collections.Generic;using System.Linq;using Syste ...
- .net工具类 获取枚举类型的描述
一般情况我们会用枚举类型来存储一些状态信息,而这些信息有时候需要在前端展示,所以需要展示中文注释描述. 为了方便获取这些信息,就封装了一个枚举扩展类. /// <summary> /// ...
- 枚举Enum转换为List,获取枚举的描述
代码: public class EnumberHelper { public static List<EnumberEntity> EnumToList<T>() { Lis ...
随机推荐
- IOS 二维码生成
这篇博客将会介绍二维码的生成. 由于没有什么东西值得长篇大论的,所以这里我就通过代码的实现介绍二维码. 第一部分 第一部分是二维码的简单生成没有其他重点介绍. 效果图 代码部分 // // ViewC ...
- 【原】UI随设备旋转从iOS6到iOS8的适配策略
- (void)statusBarOrientationChange:(NSNotification *)notification { WClassAndFunctionName; UIInterfa ...
- iOS8以后 UISearchController的用法
查了不少资料,都不太全,自己查看了apple文档,写了一份代码: 如下(只是界面): 1. 声明属性 @property (nonatomic, strong) UISearchController ...
- Mac下Apache Tomcat安装配置
Java Web如果稍微知道一点,一般对Tomcat都不会陌生,Apache是普通服务器,本身只支持html即普通网页,可以通过插件支持PHP,还可以与Tomcat连通(单向Apache连接Tomca ...
- MySQL学习总结(摘抄)
1.数据库概述 简 而言之,数据库(DataBase)就是一个存储数据的仓库.为了方便数据的存储和管理,将数据按照特定的规律存储在磁盘上.通过数据库管理系统,可以有 效的组织和管理存储在数据库中的数据 ...
- IOS中文版资源库
Swift 语言写成的项目会被标记为 ★ ,AppleWatch 的项目则会被标记为 ▲. [转自]https://github.com/jobbole/awesome-ios-cn#librari ...
- LeetCode 3 Longest Substring Without Repeating Characters(最长不重复子序列)
题目来源:https://leetcode.com/problems/longest-substring-without-repeating-characters/ Given a string, f ...
- UVa 102 - Ecological Bin Packing(规律,统计)
题目来源:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=3&pa ...
- HTML页面定时跳转方法
1)html的实现 <head> <meta http-equiv="refresh" content="5;url=hello.html"& ...
- PHPExcel中open_basedir restriction in effect的解决方法
用PHPExcel做导出execl的时候发现在本地没有问题,但是把网站传到租用的服务器的时候就报错,具体如下: Warning: realpath() [function.realpath]: ope ...