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的更多相关文章

  1. 第三章 EnumUtil根据值获取枚举对象

    项目中使用枚举类的好处这里不再赘述,在使用枚举值时,通常需要根据值来获取枚举对象,下面介绍两种实现方案: 1.在枚举类中定义方法实现 首先给出如下性别枚举类: public enum SexEnum ...

  2. EnumUtil 链表转换工具类

    package com.das.common.util; import org.springframework.util.CollectionUtils; import java.lang.refle ...

  3. .net工具类

    ConvertHelper public class ConvertHelper { /// <summary> /// 转换类型 /// </summary> /// < ...

  4. Java 005 枚举

    枚举概述:就是有有限值的集合或类.是指将变量的值一一列出来, 变量的值只限于列举出来的值得范围. 举例: 一周7天, 一年12个月等.回想单列设计模式: 单例类是一个类只有一个实例.那么多例类就是一个 ...

  5. c#项目架构搭建经验

    读过.Net项目中感觉代码写的不错(备注1)有:bbsMax(可惜唧唧喳喳鸟像消失了一样),Umbraco(国外开源的cms项目),Kooboo(国内做开源cms).本人狭隘,读的代码不多,范围也不广 ...

  6. “PMS-基础权限管理系统”实施某谱OA系统经验总结

    “PMS-基础权限管理系统”介绍 "PMS-基础权限管理系统"是我一直想做的一个产品,融合多年开发及维护管理系统的经验,参考了很多系统,精心研制而成. 可以做为毕业设计参考,新手学 ...

  7. Enum枚举 简单的使用

    在枚举中使用抽象方法 /** * 为枚举类定义一个抽象方法,<br/> * 这个抽象方法由不同的枚举值提供不同的实现 * * @author wangzhu * @date 2014-9- ...

  8. 使用Newtonsoft.Json序列化和反序列化对象(源码)

    Json数据格式,简单而强大. 使用Json,不得不提到Newtonsoft.Json,它帮助我们更方便的使用Json,当然,不使用它也是可以的,还有许多方法将对象序列化成Json字符串,暂且不提. ...

  9. MVC View返回list列表

    );             Sql sql2 = );             Sql sql3 = );             Sql sql4 = );             Sql sql ...

随机推荐

  1. 1.angular之Hello World

    <script type="text/javascript" src="http://sandbox.runjs.cn/uploads/rs/271/qrv291e ...

  2. Android面试之HashMap的实现原理

    1.HashMap与HashTable的区别 HashMap允许key和value为null: HashMap是非同步的,线程不安全,也可以通过Collections.synchronizedMap( ...

  3. DVWA默认用户名密码

    有些东西不好找啊,自己动手丰衣足食-- DVWA默认的用户有5个,用户名密码如下(一个足以): admin/password gordonb/abc123 1337/charley pablo/let ...

  4. Android调用系统软键盘

    /** * * @MethodName:closeInputMethod * @Description:关闭系统软键盘 * @throws */ public void closeInputMetho ...

  5. Raid介绍

    https://wsgzao.github.io/post/raid/ http://www.cnblogs.com/Bob-FD/p/3409221.html

  6. sublime 技巧与快捷键篇

    技巧大全:https://www.zhihu.com/question/24896283   项目排除文件夹,更便于ctrl + p的搜索,比如可恶的node_modules "folder ...

  7. php分享三十:php版本选择

    思考: cgi是怎么运行的?(是多线程?多进程?单线程?单进程?) fastcgi运行原理? apache运行php的原理? (是多进程还是多线程?) nginx是怎么运行php的? 什么是安全模式和 ...

  8. 不要随便使用 runAllManagedModulesForAllRequests="true" 来解决问题

    在 IIS 7.0 中,对于使用 Url 路由 访问页面的 ASP.NET 应用程序,IIS可能会不能出 Url 是对 ASP.NET 的请求. 会显示404啊,403啊之类的错误代码(因为路径不存在 ...

  9. 四、s3c2440 裸机开发 通用异步收发器UARN

    四.通用异步收发器UARN 原文地址 http://blog.csdn.net/woshidahuaidan2011/article/details/51137047 by jaosn Email: ...

  10. vim7.4版本在windows下的配置文件及所在位置

    1.vim在windows下默认首先会查找"_vimrc"文件,如果没有则会找".vimrc".造成这个原因是windows早期不支持以点开头的文件及目录.2. ...