关键代码:
usingSystem;usingSystem.Collections;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Reflection;namespaceCSharpUtilHelpV2
{/// <summary>/// 基于.NET 2.0的枚举工具类/// </summary>publicstaticclassEnumToolV2
{/// <summary>/// 从枚举中获取Description/// 说明:/// 单元测试-->通过/// </summary>/// <param name="enumName">需要获取枚举描述的枚举</param>/// <returns>描述内容</returns>publicstaticstringGetDescription(thisEnum 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>publicstaticDescriptionAttribute[] GetDescriptAttr(thisFieldInfo fieldInfo)
{if(fieldInfo !=null)
{return(DescriptionAttribute[])fieldInfo.GetCustomAttributes(typeof(DescriptionAttribute),false);
}returnnull;
}/// <summary>/// 根据Description获取枚举/// 说明:/// 单元测试-->通过/// </summary>/// <typeparam name="T">枚举类型</typeparam>/// <param name="description">枚举描述</param>/// <returns>枚举</returns>publicstaticT GetEnumName<T>(stringdescription)
{
Type _type =typeof(T);foreach(FieldInfo fieldin_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);
}
}thrownewArgumentException(string.Format("{0} 未能找到对应的枚举.", description),"Description");
}/// <summary>/// 将枚举转换为ArrayList/// 说明:/// 若不是枚举类型,则返回NULL/// 单元测试-->通过/// </summary>/// <param name="type">枚举类型</param>/// <returns>ArrayList</returns>publicstaticArrayList ToArrayList(thisType type)
{if(type.IsEnum)
{
ArrayList _array =newArrayList();
Array _enumValues = Enum.GetValues(type);foreach(Enumvaluein_enumValues)
{
_array.Add(newKeyValuePair<Enum,string>(value, GetDescription(value)));
}return_array;
}returnnull;
}
}
}
.csharpcode, .csharpcodePRe
{
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; }单元测试代码:
usingMicrosoft.VisualStudio.TestTools.UnitTesting;usingSystem;usingSystem.Collections;usingSystem.Collections.Generic;namespaceCSharpUtilHelpV2.Test
{publicenumTestEnum
{
[System.ComponentModel.Description("第一")]
One,
[System.ComponentModel.Description("第二")]
Two,
[System.ComponentModel.Description("第三")]
Three,
[System.ComponentModel.Description("第五")]
Five,
[System.ComponentModel.Description("全部")]
All
}
[TestClass()]publicclassEnumToolV2Test
{
[TestMethod()]publicvoidGetDescriptionTest()
{string_actual = TestEnum.Five.GetDescription();string_expected ="第五";
Assert.AreEqual(_expected, _actual);
}
[TestMethod()]publicvoidGetEnumNameTest()
{
TestEnum _actual = EnumToolV2.GetEnumName<TestEnum>("第五");
TestEnum _expected = TestEnum.Five;
Assert.AreEqual<TestEnum>(_expected, _actual);
}
[TestMethod()]publicvoidToArrayListTest()
{
ArrayList _actual = EnumToolV2.ToArrayList(typeof(TestEnum));
ArrayList _expected =newArrayList(5);
_expected.Add(newKeyValuePair<Enum,string>(TestEnum.One,"第一"));
_expected.Add(newKeyValuePair<Enum,string>(TestEnum.Two,"第二"));
_expected.Add(newKeyValuePair<Enum,string>(TestEnum.Three,"第三"));
_expected.Add(newKeyValuePair<Enum,string>(TestEnum.Five,"第五"));
_expected.Add(newKeyValuePair<Enum,string>(TestEnum.All,"全部"));
CollectionAssert.AreEqual(_expected, _actual);
}
}
}
单元测试效果:
.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; }