【转载】[C#]枚举操作(从枚举中获取Description,根据Description获取枚举,将枚举转换为ArrayList)工具类
关键代码:
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Reflection; namespace CSharpUtilHelpV2
{
/// <summary>
/// 基于.NET 2.0的枚举工具类
/// </summary>
public static class EnumToolV2
{
/// <summary>
/// 从枚举中获取Description
/// 说明:
/// 单元测试-->通过
/// </summary>
/// <param name="enumName">需要获取枚举描述的枚举</param>
/// <returns>描述内容</returns>
public static string GetDescription(this Enum enumName)
{
string _description = string.Empty;
FieldInfo _fieldInfo = enumName.GetType().GetField(enumName.ToString());
DescriptionAttribute[] _attributes = _fieldInfo.GetDescriptAttr();
if (_attributes != null && _attributes.Length > 0)
_description = _attributes[0].Description;
else
_description = enumName.ToString();
return _description;
}
/// <summary>
/// 获取字段Description
/// </summary>
/// <param name="fieldInfo">FieldInfo</param>
/// <returns>DescriptionAttribute[] </returns>
public static DescriptionAttribute[] GetDescriptAttr(this FieldInfo fieldInfo)
{
if (fieldInfo != null)
{
return (DescriptionAttribute[])fieldInfo.GetCustomAttributes(typeof(DescriptionAttribute), false);
}
return null;
}
/// <summary>
/// 根据Description获取枚举
/// 说明:
/// 单元测试-->通过
/// </summary>
/// <typeparam name="T">枚举类型</typeparam>
/// <param name="description">枚举描述</param>
/// <returns>枚举</returns>
public static T GetEnumName<T>(string description)
{
Type _type = typeof(T);
foreach (FieldInfo field in _type.GetFields())
{
DescriptionAttribute[] _curDesc = field.GetDescriptAttr();
if (_curDesc != null && _curDesc.Length > 0)
{
if (_curDesc[0].Description == description)
return (T)field.GetValue(null);
}
else
{
if (field.Name == description)
return (T)field.GetValue(null);
}
}
throw new ArgumentException(string.Format("{0} 未能找到对应的枚举.", description), "Description");
}
/// <summary>
/// 将枚举转换为ArrayList
/// 说明:
/// 若不是枚举类型,则返回NULL
/// 单元测试-->通过
/// </summary>
/// <param name="type">枚举类型</param>
/// <returns>ArrayList</returns>
public static ArrayList ToArrayList(this Type type)
{
if (type.IsEnum)
{
ArrayList _array = new ArrayList();
Array _enumValues = Enum.GetValues(type);
foreach (Enum value in _enumValues)
{
_array.Add(new KeyValuePair<Enum, string>(value, GetDescription(value)));
}
return _array;
}
return null;
}
}
}
单元测试代码:
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Collections;
using System.Collections.Generic;
namespace CSharpUtilHelpV2.Test
{
public enum TestEnum
{
[System.ComponentModel.Description("第一")]
One,
[System.ComponentModel.Description("第二")]
Two,
[System.ComponentModel.Description("第三")]
Three,
[System.ComponentModel.Description("第五")]
Five,
[System.ComponentModel.Description("全部")]
All
}
[TestClass()]
public class EnumToolV2Test
{
[TestMethod()]
public void GetDescriptionTest()
{
string _actual = TestEnum.Five.GetDescription();
string _expected = "第五";
Assert.AreEqual(_expected, _actual);
} [TestMethod()]
public void GetEnumNameTest()
{
TestEnum _actual = EnumToolV2.GetEnumName<TestEnum>("第五");
TestEnum _expected = TestEnum.Five;
Assert.AreEqual<TestEnum>(_expected, _actual);
} [TestMethod()]
public void ToArrayListTest()
{
ArrayList _actual = EnumToolV2.ToArrayList(typeof(TestEnum));
ArrayList _expected = new ArrayList(5);
_expected.Add(new KeyValuePair<Enum, string>(TestEnum.One, "第一"));
_expected.Add(new KeyValuePair<Enum, string>(TestEnum.Two, "第二"));
_expected.Add(new KeyValuePair<Enum, string>(TestEnum.Three, "第三"));
_expected.Add(new KeyValuePair<Enum, string>(TestEnum.Five, "第五"));
_expected.Add(new KeyValuePair<Enum, string>(TestEnum.All, "全部"));
CollectionAssert.AreEqual(_expected, _actual); }
}
}
单元测试效果:
【转载】[C#]枚举操作(从枚举中获取Description,根据Description获取枚举,将枚举转换为ArrayList)工具类的更多相关文章
- 时间工具类之“ JDK1.8中 LocalDate、LocalTime、LocalDateTime、LocalDateTimeUtil四个时间工具类”
一.使用的原因 在JDK8发布的时候,推出了LocalDate.LocalTime.LocalDateTime这个三个时间处理类,以此来弥补之前的日期时间类的不足,简化日期时间的操作. 在Java8之 ...
- Java中使用google.zxing快捷生成二维码(附工具类源码)
移动互联网时代,基于手机端的各种活动扫码和收付款码层出不穷:那我们如何在Java中生成自己想要的二维码呢?下面就来讲讲在Java开发中使用 google.zxing 生成二维码. 一般情况下,Java ...
- (转)Android中px与dip,sp与dip等的转换工具类
功能 通常在代码中设置组件或文字大小只能用px,通过这个工具类我们可以把dip(dp)或sp为单位的值转换为以px为单位的值而保证大小不变.方法中的参数请参考http://www.cnblogs.co ...
- Java 获取webapp,Root,classpath,项目等路径工具类
public class UtilPath { public static void main(String[] args) { String systemName = System.getPrope ...
- Android自定义工具类获取按钮并绑定事件(利用暴力反射和注解)
Android中为按钮绑定事件的有几种常见方式,你可以在布局文件中为按钮设置id,然后在MainActivity中通过findViewById方法获取按钮对象实例,再通过setOnClickListe ...
- (转载)android 一些工具类汇总
android 一些工具类汇总 作者:曾田生z 字体:[增加 减小] 类型:转载 时间:2016-08-14我要评论 本文给大家汇总介绍了一些常用的Android工具类,非常的简单实用,有需要的小伙伴 ...
- [Google Guava] 2.3-强大的集合工具类:java.util.Collections中未包含的集合工具
原文链接 译文链接 译者:沈义扬,校对:丁一 尚未完成: Queues, Tables工具类 任何对JDK集合框架有经验的程序员都熟悉和喜欢java.util.Collections包含的工具方法.G ...
- 【转载】 C#工具类:Csv文件转换类
CSV是逗号分隔值格式的文件,其文件以纯文本形式存储表格数据(数字和文本).CSV文件由任意数目的记录组成,记录间以某种换行符分隔:每条记录由字段组成,字段间的分隔符是其它字符或字符串,最常见的是逗号 ...
- java高并发系列 - 第16天:JUC中等待多线程完成的工具类CountDownLatch,必备技能
这是java高并发系列第16篇文章. 本篇内容 介绍CountDownLatch及使用场景 提供几个示例介绍CountDownLatch的使用 手写一个并行处理任务的工具类 假如有这样一个需求,当我们 ...
- 【转载】Asp.Net生成图片验证码工具类
在Asp.Net应用程序中,很多时候登陆页面以及其他安全重要操作的页面需要输入验证码,本文提供一个生成验证码图片的工具类,该工具类通过随机数生成验证码文本后,再通过C#中的图片处理类位图类,字体类,一 ...
随机推荐
- Entity Framework返回IEnumerable还是IQueryable?
在使用EF的过程中,我们常常使用repository模式,本文就在repository层的返回值是IEnumerable类型还是IQueryable进行探讨. 阅读目录: 一.什么是Repositor ...
- 【T-SQL基础】01.单表查询-几道sql查询题
概述: 本系列[T-SQL基础]主要是针对T-SQL基础的总结. [T-SQL基础]01.单表查询-几道sql查询题 [T-SQL基础]02.联接查询 [T-SQL基础]03.子查询 [T-SQL基础 ...
- [.net 面向对象编程基础] (17) 数组与集合
[.net 面向对象编程基础] (17) 数组与集合 学习了前面的C#三大特性,及接口,抽象类这些相对抽象的东西以后,是不是有点很累的感觉.具体的东西总是容易理解,因此我们在介绍前面抽象概念的时候,总 ...
- css实现左栏固定右栏自适应,高度自适应的布局
收集css中的基础知识,所以这并不是什么新鲜的技术,只是作为备忘~本文的内容如题所示,是一个简单的布局,用于左右两栏布局的页面,左侧是固定宽度,右侧占据剩余的宽度.在垂直方向,始终以高度最大的一栏为基 ...
- 使用NodeList
理解NodeList.NamedNodeMap和HTMLCollection是整体透彻理解DOM的关键. 这三个集合都是“动态”的,也就是说:每当文档结构发生变化时,他们都会得到更新,他们始终保存的都 ...
- Unity3D核心类型一览
Unity3D核心类型一览 本文记录了Unity3D的最基本的核心类型.包括Object.GameObject.Component.Transform.Behaviour.Renderer.Colli ...
- JavaScript 误区
接触JavaScript两年多遇到过各种错误,其中有一些让人防不胜防,原来对JavaScript的误会如此之深,仅以此文总结一下常见的各种想当然的误区 String replace string的re ...
- 在同一台服务器上配置多个Tomcat
如果要在一台服务器上配置多个Tomcat,主要就是要避免Tomcat服务器的端口冲突的问题.只需要修改CATALINA_HOME\conf\server.xml中的启动端口和连接端口就OK了! 下面我 ...
- JSONP浅析
DEMO : JSONP示例 为什么使用JSONP JSONP和JSON是不一样的.JSON(JavaScript Object Notation)是一种基于文本的数据交换方式,或者叫做数据描述格式. ...
- Atitit vod click event design flow 视频点播系统点击事件文档
Atitit vod click event design flow 视频点播系统点击事件文档 重构规划1 Click cate1 Click mov4 重构规划 事件注册,与事件分发管理器分开 ...
