关键代码:
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Reflection;
namespace CSharpUtilHelpV2
{
///
/// 基于.NET 2.0的枚举工具类
///
public static class EnumToolV2
{
///
/// 从枚举中获取Description
/// 说明:
/// 单元测试-->通过
///
/// 需要获取枚举描述的枚举
/// 描述内容
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;
}
///
/// 获取字段Description
///
/// FieldInfo
/// DescriptionAttribute[]
public static DescriptionAttribute[] GetDescriptAttr(this FieldInfo fieldInfo)
{
if (fieldInfo != null)
{
return (DescriptionAttribute[])fieldInfo.GetCustomAttributes(typeof(DescriptionAttribute), false);
}
return null;
}
///
/// 根据Description获取枚举
/// 说明:
/// 单元测试-->通过
///
/// 枚举类型
/// 枚举描述
/// 枚举
public static T GetEnumName(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");
}
///
/// 将枚举转换为ArrayList
/// 说明:
/// 若不是枚举类型,则返回NULL
/// 单元测试-->通过
///
/// 枚举类型
/// ArrayList
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(value, GetDescription(value)));
}
return _array;
}
return null;
}
}
}