首页 > 代码库 > [C#]枚举操作(从枚举中获取Description,根据Description获取枚举,将枚举转换为ArrayList)工具类
[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; } }}
<style type="text/css">.csharpcode, .csharpcode pre{ font-size: small; color: black; font-family: consolas, "Courier New", courier, monospace; background-color: #ffffff; /*white-space: pre;*/}.csharpcode pre { margin: 0em; }.csharpcode .rem { color: #008000; }.csharpcode .kwrd { color: #0000ff; }.csharpcode .str { color: #006080; }.csharpcode .op { color: #0000c0; }.csharpcode .preproc { color: #cc6633; }.csharpcode .asp { background-color: #ffff00; }.csharpcode .html { color: #800000; }.csharpcode .attr { color: #ff0000; }.csharpcode .alt { background-color: #f4f4f4; width: 100%; margin: 0em;}.csharpcode .lnum { color: #606060; }</style>单元测试代码:
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); } }}单元测试效果:<style type="text/css">.csharpcode, .csharpcode pre{ font-size: small; color: black; font-family: consolas, "Courier New", courier, monospace; background-color: #ffffff; /*white-space: pre;*/}.csharpcode pre { margin: 0em; }.csharpcode .rem { color: #008000; }.csharpcode .kwrd { color: #0000ff; }.csharpcode .str { color: #006080; }.csharpcode .op { color: #0000c0; }.csharpcode .preproc { color: #cc6633; }.csharpcode .asp { background-color: #ffff00; }.csharpcode .html { color: #800000; }.csharpcode .attr { color: #ff0000; }.csharpcode .alt { background-color: #f4f4f4; width: 100%; margin: 0em;}.csharpcode .lnum { color: #606060; }</style>
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。