EnumUtil
EnumUtil.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Reflection; namespace EnumExtensions
{
/// <summary>
/// 枚举实用操作
/// </summary>
public static class EnumUtil
{
/// <summary>
/// 把枚举转换为键值对集合
/// </summary>
/// <param name="enumType">枚举类型</param>
/// <param name="getText">以Enum为参数类型,String为返回类型的委托</param>
/// <returns>以枚举值为key,枚举文本为value的键值对集合</returns>
public static Dictionary<Int32, String> EnumToDictionary(Type enumType, Func<Enum, String> getText)
{
if (!enumType.IsEnum)
{
throw new ArgumentException("传入的参数必须是枚举类型!", "enumType");
}
Dictionary<Int32, String> enumDic = new Dictionary<int, string>();
Array enumValues = Enum.GetValues(enumType);
foreach (Enum enumValue in enumValues)
{
Int32 key = Convert.ToInt32(enumValue);
String value = getText(enumValue);
enumDic.Add(key, value);
}
return enumDic;
} /// <summary>
/// 在指定枚举中检索具有指定值的描述信息
/// </summary>
/// <param name="enumType">枚举类型</param>
/// <param name="value">特定枚举常数的值(根据其基础类型)</param>
/// <param name="nameInstead">当枚举值没有定义DescriptionAttribute,是否使用枚举名代替,默认是使用</param>
/// <returns>enumType的枚举常数的描述信息,如果没有找到这样的枚举常数,则为null。</returns>
public static String GetDescription(Type enumType, object value, Boolean nameInstead = true)
{
Enum e = (Enum)Enum.ToObject(enumType, value);
return e == null ? null : e.GetDescription(nameInstead);
} /// <summary>
/// 扩展方法,获得枚举的Description
/// </summary>
/// <param name="value">枚举值</param>
/// <param name="nameInstead">当枚举值没有定义DescriptionAttribute,是否使用枚举名代替,默认是使用</param>
/// <returns>枚举的Description</returns>
public static string GetDescription(this Enum value, Boolean nameInstead = true)
{
Type type = value.GetType();
string name = Enum.GetName(type, value);
if (name == null)
{
return null;
} FieldInfo field = type.GetField(name);
DescriptionAttribute attribute = Attribute.GetCustomAttribute(field, typeof(DescriptionAttribute)) as DescriptionAttribute; if (attribute == null&&nameInstead == true)
{
return name;
}
return attribute == null ? null : attribute.Description;
}
}
}
调用方式Program.cs
using System;
using System.Collections.Generic;
using System.ComponentModel; namespace EnumExtensions
{
public enum Season
{
[Description("春 天")]
Spring = ,
[Description("夏 天")]
Summer = ,
//[Description("秋 天")]
Autumn = ,
[Description("冬 天")]
Winter =
}
class Program
{
static void Main(string[] args)
{
Season spring = Season.Spring;
//打印枚举名
Console.WriteLine(spring.ToString());
//打印枚举说明
Console.WriteLine(spring.GetDescription()); //枚举转换为键值对集合
Dictionary<Int32, String> dic = EnumUtil.EnumToDictionary(typeof(Season), e => e.GetDescription());
PrintDic(dic); dic = EnumUtil.EnumToDictionary(typeof(Season), e => e.GetDescription(false));
PrintDic(dic); dic = EnumUtil.EnumToDictionary(typeof(Season), e => e.ToString());
PrintDic(dic); dic = EnumUtil.EnumToDictionary(typeof(Season), e => Enum.GetName(typeof(Season), e));
PrintDic(dic); Console.ReadLine();
} private static void PrintDic(Dictionary<Int32, String> dic)
{
foreach (KeyValuePair<Int32,String> item in dic)
{
Console.WriteLine("Key:{0}-----Value:{1}", item.Key, item.Value);
}
}
}
}
EnumUtil的更多相关文章
- 第三章 EnumUtil根据值获取枚举对象
项目中使用枚举类的好处这里不再赘述,在使用枚举值时,通常需要根据值来获取枚举对象,下面介绍两种实现方案: 1.在枚举类中定义方法实现 首先给出如下性别枚举类: public enum SexEnum ...
- EnumUtil 链表转换工具类
package com.das.common.util; import org.springframework.util.CollectionUtils; import java.lang.refle ...
- .net工具类
ConvertHelper public class ConvertHelper { /// <summary> /// 转换类型 /// </summary> /// < ...
- Java 005 枚举
枚举概述:就是有有限值的集合或类.是指将变量的值一一列出来, 变量的值只限于列举出来的值得范围. 举例: 一周7天, 一年12个月等.回想单列设计模式: 单例类是一个类只有一个实例.那么多例类就是一个 ...
- c#项目架构搭建经验
读过.Net项目中感觉代码写的不错(备注1)有:bbsMax(可惜唧唧喳喳鸟像消失了一样),Umbraco(国外开源的cms项目),Kooboo(国内做开源cms).本人狭隘,读的代码不多,范围也不广 ...
- “PMS-基础权限管理系统”实施某谱OA系统经验总结
“PMS-基础权限管理系统”介绍 "PMS-基础权限管理系统"是我一直想做的一个产品,融合多年开发及维护管理系统的经验,参考了很多系统,精心研制而成. 可以做为毕业设计参考,新手学 ...
- Enum枚举 简单的使用
在枚举中使用抽象方法 /** * 为枚举类定义一个抽象方法,<br/> * 这个抽象方法由不同的枚举值提供不同的实现 * * @author wangzhu * @date 2014-9- ...
- 使用Newtonsoft.Json序列化和反序列化对象(源码)
Json数据格式,简单而强大. 使用Json,不得不提到Newtonsoft.Json,它帮助我们更方便的使用Json,当然,不使用它也是可以的,还有许多方法将对象序列化成Json字符串,暂且不提. ...
- MVC View返回list列表
); Sql sql2 = ); Sql sql3 = ); Sql sql4 = ); Sql sql ...
随机推荐
- gitignore file already add
忽略一些已经添加到Git版本管理的文件 先用 git remove --cache filename 再将文件加入.gitignore文件
- Markdown 使用教程
前言 以前经常在 github 中看到 .md 格式的文件,一直没有注意,也不明白为什么文本文档的后缀不是 .txt ,后来无意中看到了 Markdown,看到了用这个东西写得一些web界面等特别的规 ...
- Android自带的TTS功能
在Android1.6之后添加了TextToSpeech,也叫TTS,把相应的文字转化成语音播报,增强了用户体验.可以根据语言播报 界面上的控件如下: 可以选择的语言 但有的语言不支持,比如中文就不支 ...
- jmeter 正则获取参数集合和ForEach控制器结合使用
怎么把第一个请求获取的返回的多个id,在第二个请求中逐个以单个id作为请求参数来请求? 为了解决这个问题,模拟下该场景 1.请求www.163.com 主页,获取响应中的所有数字,这个获取的数字集合暂 ...
- 【转载】Spring @Async 源码解读。
由于工作中经常需要使用到异步操作,一直在使用@Async, 今天抽空学习了一下它的执行原理,刚好看到一篇写的很棒的文章,这里转载过来做个记录,感谢原作者的无私奉献. 原文章链接地址:https://w ...
- 使用git上传项目到码云
一.git安装 1.首先在官方网站下载git工具,或者根据以下链接进行下载:http://download.csdn.net/detail/qq_27501889/9788879(此链接版本为git- ...
- 两table水平滚动条级联滚动(同步滚动)。 table1放标题,table2放内容。
//table1=head和table2=body水平滚动条级联滚动 $(document).ready(function () { $("#bodyPanel").scroll( ...
- 如何使用 Chrome 浏览器调试动态加载的 Javascript 脚本
在IE中,可以在调试程序的文档列表最下方看到一个"动态脚本"的文件夹,里面可以找到动态加载的脚本,但是...数量繁多,也不能自定义名称... 但是在 Chrome 中,貌似根本找不 ...
- MYSQL拒绝访问:not allowed to connect解决方法
分享下MYSQL拒绝访问报错not allowed to connect的解决方法. 可以在其它任何的主机上以root身份登录 mysql报如下错误,截取部分, message from server ...
- IOS解惑(1)之@property(nonatomic,getter=isOn) BOOL on;中的getter解惑
1 问题: @property(nonatomic,getter=isOn) BOOL on; 中的getter = isOn的含义? 2 答案: 如果这个property是 BOOL on, 那么O ...