码迷,mamicode.com
首页 > Windows程序 > 详细

c# 枚举返回字符串操作

时间:2016-07-25 15:52:58      阅读:313      评论:0      收藏:0      [点我收藏+]

标签:

//内部类
public static class EnumHelper { public static string GetDescription(Enum value) { if (value == null) { throw new ArgumentException("value"); } string description = value.ToString(); var fieldInfo = value.GetType().GetField(description); var attributes = (EnumDescriptionAttribute[]) fieldInfo.GetCustomAttributes(typeof (EnumDescriptionAttribute), false); if (attributes != null && attributes.Length > 0) { description = attributes[0].Description; } return description; } }
// 枚举定义
// 调用方法
String strKufen = EnuHelper.GetDescription(MessageInfo.Send);  public enum MessageInfo { [EnumDescription("Send")] Send, [EnumDescription("Received")] Received } [AttributeUsage(AttributeTargets.Field, AllowMultiple = false)] public sealed class EnumDescriptionAttribute : Attribute { private string description; public string Description { get { return description; } } public EnumDescriptionAttribute(string description) : base() { this.description = description; } }

 

c# 枚举返回字符串操作

标签:

原文地址:http://www.cnblogs.com/northeastTycoon/p/5703583.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!