首页 > 学院 > 开发设计 > 正文

[C#]枚举操作(从枚举中获取Description,根据Description获取枚举,将枚举转换为ArrayList)工具类

2019-11-14 16:37:35
字体:
来源:转载
供稿:网友

关键代码:

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);        }    }}单元测试效果:
image

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表