获取枚举类型的描述description
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks; namespace candel
{
class Program
{
static void Main(string[] args)
{
int x = (int)OrderType.WaitConfirm;
string str = Util.GetEnumDesc(typeof(OrderType), x);
Console.WriteLine(str); Order o = new Order() { Id = , Type = (int)OrderType.Complete };
string str2 = (typeof(OrderType)).GetEnumDesc(o.Type);
Console.WriteLine(str2); Console.Read();
} } public class Order
{
public int Id { set; get; }
public int Type { set; get; }
} public enum OrderType
{
/// <summary>
/// 等待用户付款
/// </summary>
[Description("等待用户付款")]
WaitPay = ,
/// <summary>
/// 等待商家发货
/// </summary>
[Description("等待商家发货")]
WaitSend = ,
/// <summary>
/// 等待用户确认收货
/// </summary>
[Description("等待用户确认收货")]
WaitConfirm = ,
/// <summary>
/// 订单完成
/// </summary>
[Description("订单完成")]
Complete =
} public static class Util
{
/// <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 "";
}
}
}
获取枚举类型的描述description的更多相关文章
- .net工具类 获取枚举类型的描述
一般情况我们会用枚举类型来存储一些状态信息,而这些信息有时候需要在前端展示,所以需要展示中文注释描述. 为了方便获取这些信息,就封装了一个枚举扩展类. /// <summary> /// ...
- 获取枚举值上的Description特性说明
/// <summary> /// 获取枚举值上的Description特性说明 /// </summary> /// <typeparam name="T&q ...
- 获取枚举类型Description特性的描述信息
C#中可以对枚举类型用Description特性描述. 如果需要对Description信息获取,那么可以定义一个扩展方法来实现.代码如下: public static class EnumExten ...
- C# 枚举类型的描述信息获取
新建一个控制台方法,写好自己的枚举类型: 如图: 在里面添加获取描述的方法: 具体源码: 链接:http://pan.baidu.com/s/1nv4rGkp 密码:byz8
- C#枚举扩展方法,获取枚举值的描述值以及获取一个枚举类下面所有的元素
/// <summary> /// 枚举扩展方法 /// </summary> public static class EnumExtension { private stat ...
- 反射:获取枚举类型的Name,Value,Description
[Obsolete("请使用新的方法XXX")] //使用Obsolete特性来告诉使用者这是一个过期的方法 private static void Test() { Type t ...
- 给枚举加上Description,必要时,可以直接获取枚举类型代表的中文
http://www.cnblogs.com/lyl6796910/p/3958768.html
- C# 获取枚举的描述Description
方法类: public static class EnumExtensions { #region Enum /// <summary> /// 获取枚举变量值的 Description ...
- 获取枚举Name,Value,Description两种方法
using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.UI ...
随机推荐
- jQuery学习总结(一)
jQuery当中独有的对象:jQuery对象: jQuery对象的缩写形式“$”:所以在使用时,我们都是用$来代替jQuery. 所以我们在页面元素选择或执行功能函数的时候可以这么写:$(functi ...
- 天气预报数据API
http://www.weather.com.cn/data/cityinfo/101010100.html//过期: http://api.36wu.com/Weather/GetMoreWeath ...
- 产品中 configure/cross compile的一个bug
在mac机上, 为iPhone版本编译产品. 运行./configure报错如下: configure:22793: error: cannot run test program while cros ...
- mysql 授权
cd /usr/local/mysql/bin/grant all privileges on *.* to 'root'@'%' identified by '12345678';flush pri ...
- emum类(2)
emum定义如下: public abstract class Enum<E extends Enum<E>>extends Objectimplements Comparab ...
- 用c#开发微信 (16) 微活动 2 刮刮卡
微信营销是一种新型的营销模式,由于微信更重视用户之间的互动,故而这种营销推广不不能盲目地套用微博营销的单纯大量广告推送方式.这种方式在微信营销中的效果非常差,会令用户反感,继而取消去企业或商家的微信公 ...
- CBA 赛程的笔记 - 北京首钢
2014-11-01 19:35 北京首钢 103:89 广东宏远 结束 技术统计 发挥不错,打的比较好! 2014-11-05 19:35 八一双鹿 89:100 北京首钢 结束 技术统计 第一 ...
- 在Android中调用C#写的WebService(附源代码)
由于项目中要使用Android调用C#写的WebService,于是便有了这篇文章.在学习的过程中,发现在C#中直接调用WebService方便得多,直接添加一个引用,便可以直接使用将WebServi ...
- AngularJS快速入门指南06:过滤器
thead>tr>th, table.reference>tbody>tr>th, table.reference>tfoot>tr>th, table ...
- Ubuntu命令--CURL用法
curl命令是个功能强大的网络工具,支持通过http.ftp等方式下载文件.上传文件.还可以用来抓取网页.网络监控等方面的开发,解决开发过程中遇到的问题. 常用参数curl命令参数很多,这里只列出我曾 ...