zoukankan      html  css  js  c++  java
  • 【转载】[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);
    
            }
        }
    }
    单元测试效果:
    image
  • 相关阅读:
    【阿里聚安全·安全周刊】苹果证实 iOS 源代码泄露|英国黑客赢下官司
    150万元重奖!阿里软件供应链安全大赛正式启动
    【阿里聚安全·安全周刊】山寨外挂有风险养蛙需谨慎|健身追踪热度图爆军事基地位置
    移动APP外挂攻防实战
    阿里云正式上线移动直播问答解决方案,助力APP尽情“撒币”!
    阿里安全资深专家杭特辣评中国网络安全人才之“怪现状”
    【阿里聚安全·安全周刊】Intel芯片级安全漏洞事件|macOS存在漏洞
    阿里聚安全年终盘点|2017互联网安全领域十大话题
    【技术分析】DowginCw病毒家族解析
    独家探寻阿里安全潘多拉实验室,完美越狱苹果iOS11.2.1
  • 原文地址:https://www.cnblogs.com/hycms/p/4127751.html
Copyright © 2011-2022 走看看