usingSystem;usingSystem.ComponentModel;usingSystem.Reflection;namespaceLH.DotNet2.Utilities.Common
{/// <summary>/// 枚举帮助类/// </summary>publicstaticclassEnumHelper
{#region私有方法PRivatestaticDescriptionAttribute[] GetDescriptAttr(thisFieldInfo fieldInfo)
{if(fieldInfo !=null)
{return(DescriptionAttribute[])fieldInfo.GetCustomAttributes(typeof(DescriptionAttribute),false);
}returnnull;
}#endregion私有方法/// <summary>///判断枚举是否包括枚举常数名称/// </summary>/// <typeparam name="T">枚举</typeparam>/// <param name="enumName">枚举常数名称</param>/// <returns>是否包括枚举常数名称</returns>/// 创建 人:言志伟/// 创建时间:2015-06-04 17:17/// 备注说明:<c>null</c>publicstaticboolCheckedContainEnumName<T>(stringenumName)whereT :struct, IConvertible
{bool_result =false;if(typeof(T).IsEnum)
{string[] _enumnName = Enum.GetNames(typeof(T));if(_enumnName !=null)
{for(inti = 0; i < _enumnName.Length; i++)
{if(string.Compare(_enumnName[i], enumName,true) == 0)
{
_result =true;
break;
}
}
}
}return_result;
}/// <summary>/// 从枚举中获取Description/// </summary>/// <param name="targetEnum">需要获取枚举描述的枚举</param>/// <returns>描述内容</returns>publicstaticstringGetDescription(thisEnum targetEnum)
{string_description =string.Empty;
FieldInfo _fieldInfo = targetEnum.GetType().GetField(targetEnum.ToString());
DescriptionAttribute[] _attributes = _fieldInfo.GetDescriptAttr();if(_attributes !=null&& _attributes.Length > 0)
_description = _attributes[0].Description;else_description = targetEnum.ToString();return_description;
}/// <summary>/// 根据Description获取枚举/// </summary>/// <typeparam name="T">枚举类型</typeparam>/// <param name="description">枚举描述</param>/// <returns>枚举</returns>publicstaticT ParseEnumDescription<T>(thisstringdescription, T defaultValue)whereT :struct, IConvertible
{if(typeof(T).IsEnum)
{
Type _type =typeof(T);foreach(FieldInfo fieldin_type.GetFields())
{
DescriptionAttribute[] _description = field.GetDescriptAttr();if(_description !=null&& _description.Length > 0)
{if(string.Compare(_description[0].Description, description,true) == 0)
{
defaultValue = (T)field.GetValue(null);
break;
}
}else{if(string.Compare(field.Name, description,true) == 0)
{
defaultValue = (T)field.GetValue(null);
break;
}
}
}
}returndefaultValue;
}/// <summary>/// 将枚举常数名称转换成枚举/// </summary>/// <typeparam name="T">枚举</typeparam>/// <param name="enumName">枚举常数名称</param>/// <returns></returns>/// 创建 人:言志伟/// 创建时间:2015-06-05 9:16/// 备注说明:<c>null</c>publicstaticT ParseEnumName<T>(thisstringenumName)whereT :struct, IConvertible
{return(T)Enum.Parse(typeof(T), enumName,true);
}
}
}
.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; }
单元测试:
usingMicrosoft.VisualStudio.TestTools.UnitTesting;namespaceYanZhiwei.DotNet2.Utilities.Common.Tests
{publicenumAreaMode
{
NONE,
CITY,
TOWN,
ROAD,
CITYTOWN,
TOWNROAD,
CITYROAD,
ALL
}
[TestClass()]publicclassEnumHelperTests
{
[TestMethod()]publicvoidCheckedContainEnumNameTest()
{
Assert.IsTrue(EnumHelper.CheckedContainEnumName<AreaMode>("none"));
}
[TestMethod()]publicvoidGetDescriptionTest()
{
Assert.AreEqual("NONE", AreaMode.NONE.GetDescription());
}
[TestMethod()]publicvoidParseEnumDescriptionTest()
{
Assert.AreEqual(AreaMode.NONE, EnumHelper.ParseEnumDescription<AreaMode>("NONE", AreaMode.CITYTOWN));
}
[TestMethod()]publicvoidParseEnumNameTest()
{
Assert.AreEqual(AreaMode.ALL, EnumHelper.ParseEnumName<AreaMode>("ALL"));
}
}
}
.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; }