C# 读取枚举描述信息实例
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Serialization.Json;
using System.IO;
using System.Data;
using System.ComponentModel;
using System.Web.UI.WebControls;
namespace SieCom.YQ.Common
{
public class CommondLibray
{
public CommondLibray()
{
//TODO
}
#region 根据枚举值获取枚举描述信息(单个)
/// <summary>
/// 根据枚举值获取枚举描述信息(单个)
/// </summary>
/// <typeparam name="TEnum">枚举</typeparam>
/// <param name="value">枚举值</param>
/// <returns></returns>
public static string GetEnumDescription<TEnum>(object value)
{
string result = string.Empty;
Type enumType = typeof(TEnum);
if (enumType.IsEnum)
{
var name = System.Enum.GetName(enumType, Convert.ToInt32(value));
if (name != null)
{
object[] objs = enumType.GetField(name).GetCustomAttributes(typeof(DescriptionAttribute), false);
if (objs != null && objs.Length > 0)
{
DescriptionAttribute attr = objs[0] as DescriptionAttribute;
result = attr.Description;
}
else
{
//TODO
}
}
else
{
//TODO
}
}
return result;
}
#endregion
#region 获取枚举列表
/// <summary>
/// 获取枚举列表
/// </summary>
/// <param name="enumType">枚举类型</param>
/// <param name="IsAll">是否在枚举列表前加入全部</param>
/// <returns>枚举对应的ListItem</returns>
public static List<ListItem> GetEnumList(Type enumType, bool IsAll)
{
List<ListItem> list = new List<ListItem>();
if (enumType.IsEnum)
{
if (IsAll)
list.Add(new ListItem("--请选择--", "-1"));
Type type = typeof(DescriptionAttribute);
System.Reflection.FieldInfo[] files = enumType.GetFields();
string strText = string.Empty;
string strValue = string.Empty;
foreach (System.Reflection.FieldInfo field in files)
{
if (field.IsSpecialName) continue;
strValue = field.GetRawConstantValue().ToString();
object[] arr = field.GetCustomAttributes(type, true);
if (arr.Length > 0)
strText = (arr[0] as DescriptionAttribute).Description;
else strText = field.Name;
list.Add(new ListItem(strText, strValue));
}
}
return list;
}
#endregion
}
}
C# 读取枚举描述信息实例的更多相关文章
- EnumHelper.cs枚举助手(枚举描述信息多语言支持)C#
C#里面经常会用到枚举类型,枚举是值类型对象,如果你想用枚举类型的多属性特性,或者你想在MVC页面上通过简单的值类型转换,将某字段值所代表的含义转换为文字显示,这时候必须要将枚举扩展,是它支持文本描述 ...
- .NET获取枚举DescriptionAttribute描述信息性能改进的多种方法
一. DescriptionAttribute的普通使用方式 1.1 使用示例 DescriptionAttribute特性可以用到很多地方,比较常见的就是枚举,通过获取枚举上定义的描述信息在UI上显 ...
- 利用DescriptionAttribute定义枚举值的描述信息 z
System.ComponentModel命名空间下有个名为DescriptionAttribute的类用于指定属性或事件的说明,我所调用的枚举值描述信息就是DescriptionAttribute类 ...
- 在C#中如何读取枚举值的描述属性
在C#中,有时候我们需要读取枚举值的描述属性,也就是说这个枚举值代表了什么意思.比如本文中枚举值 Chinese ,我们希望知道它代表意思的说明(即“中文”). 有下面的枚举: 1 2 3 4 5 6 ...
- 自己动手之使用反射和泛型,动态读取XML创建类实例并赋值
前言: 最近小匹夫参与的游戏项目到了需要读取数据的阶段了,那么觉得自己业余时间也该实践下数据相关的内容.那么从哪入手呢?因为用的是Unity3d的游戏引擎,思来想去就选择了C#读取XML文件这个小功能 ...
- MVC中得到成员元数据的Description特性描述信息公用方法
#region 从类型成员获取指定的Attribute T特性集合 /// <summary> /// 从类型成员获取指定的Attribute T特性集合 /// </summary ...
- C#通过反射进行枚举描述相关操作
C#可以通过反射,来获取枚举的描述信息或通过描述信息获取到指定类型的枚举 /// <summary> /// 获取枚举描述 /// </summary> /// <par ...
- C#获取枚举描述代码
public class MusterEnum { /// 获取枚举的描述信息 /// </summary> /// <param name="e">传入枚 ...
- C#取枚举描述
一直都觉得枚举是个很不错的东西,可以给我们带来很多方便,而且也增加代码的可读性. 我在之前已经介绍过枚举的简要应用了,再次再来写下怎么获取枚举的描述. 源码如下: 首先,我们定义个含有描述的枚举类型 ...
随机推荐
- c#扩展方法-摘自msdn
扩展方法使你能够向现有类型“添加”方法,而无需创建新的派生类型.重新编译或以其他方式修改原始类型. 扩展方法是一种特殊的静态方法,但可以像扩展类型上的实例方法一样进行调用. 对于用 C# 和 Vis ...
- dicom格式文件 界定标识符的处理
转自:http://www.cnblogs.com/assassinx/archive/2013/05/18/3084854.html 说到底无非几个事情 :1传输语法确定 2数据元素读取 3 7fe ...
- JQuery自定义属性的设置和获取
Jquery操作自定义属性的方法,很简洁: $("#test").attr("test","aaa") // 设置 $("#tes ...
- Poj OpenJudge 百练 1062 昂贵的聘礼
1.Link: http://poj.org/problem?id=1062 http://bailian.openjudge.cn/practice/1062/ 2.Content: 昂贵的聘礼 T ...
- 用jQuery解析复杂的xml结构文件
一个晚上的心血 <?xml version="1.0" encoding="UTF-8"?> <weibo><wbContent& ...
- WPF-控件-编辑圆角TextBox
使用模板 代码如下: <Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xm ...
- 获得当前时间的PRO
1.没有参数的存储过程 create or replace procedure get_timeas cur_time varchar2(10);begin select to_char(sy ...
- Python 学习教程
<Core Python Programming>勘误参考表 http://starship.python.net/crew/wesc/cpp/errata2.htm 笨办法学 Pytho ...
- Ubuntu下Apache+php+mysql网站架设详解
目录 1 基础 2 安装 2.1 安装LAMP 2.2 图形化管理软件(可选) 2.2.1 安装webmin 2.2.2 安装phpmyadmin 3 配置文件路径 3.1 常用命令 3.2 配置ap ...
- libevent使用
(sudo apt-get install libevent-dev) 1:安装libevent 用wget指令直接下载libevent:# wget http://www.monkey.org/~p ...