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

本文介绍了一个基于.NET2.0的枚举工具类,提供了从枚举获取描述、获取字段描述、根据描述获取枚举以及将枚举转换为ArrayList的功能,并通过单元测试验证了其正确性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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);

    }
  }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值