[C #] EnumHelper,

Source: Internet
Author: User

[C #] EnumHelper,

Using System; using System. componentModel; using System. reflection; namespace LH. dotNet2.Utilities. common {// <summary> /// enumeration help class /// </summary> public static class EnumHelper {# region private method private static DescriptionAttribute [] GetDescriptAttr (this FieldInfo fieldInfo) {if (fieldInfo! = Null) {return (DescriptionAttribute []) fieldInfo. getCustomAttributes (typeof (DescriptionAttribute), false);} return null ;} # endregion private method /// <summary> /// determines whether the enumeration includes the enumerated constant name /// </summary> /// <typeparam name = "T"> enumeration </typeparam> // <param name = "enumName"> enumeration constant name </param> // <returns> whether the enumerated constant name is included </returns> // create persons: yan Zhiwei // Creation Time: // remarks: <c> null </c> public static bool CheckedContainEnum Name <T> (string enumName) where T: struct, IConvertible {bool _ result = false; if (typeof (T ). isEnum) {string [] _ enumnName = Enum. getNames (typeof (T); if (_ enumnName! = Null) {for (int I = 0; I <_ enumnName. length; I ++) {if (string. compare (_ enumnName [I], enumName, true) = 0) {_ result = true; break ;}}} return _ result ;} /// <summary> /// obtain the Description from the enumeration /// </summary> /// <param name = "targetEnum"> obtain the enumeration Description </ param> // <returns> description </returns> public static string GetDescription (this Enum targetEnum) {string _ description = string. empty; FieldInfo _ fie LdInfo = 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> /// obtain enumeration Based on Description /// </summary> /// <typeparam name = "T"> Enumeration type </typeparam> /// <param name = "description"> enumeration description </param> // <returns> enumeration </returns> public static T ParseEnumDescription <T> (this string description, T defaultValue) where T: struct, IConvertible {if (typeof (T ). isEnum) {Type _ type = typeof (T); foreach (FieldInfo field in _ 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 ;}}} return defaultValue ;} /// <summary> /// convert the enumerated constant name to an enumeration // </summary> /// <typeparam name = "T"> enumeration </typeparam>/ // <param name = "enumName"> enumeration constant name </param> // <returns> </returns> // Creator: yan Zhiwei // Creation Time: // remarks: <c> null </c> public static T ParseEnumName <T> (this string enumName) where T: struct, IConvertible {return (T) Enum. parse (typeof (T), enumName, true );}}}

Unit test:

using Microsoft.VisualStudio.TestTools.UnitTesting;namespace YanZhiwei.DotNet2.Utilities.Common.Tests{    public enum AreaMode    {        NONE,        CITY,        TOWN,        ROAD,        CITYTOWN,        TOWNROAD,        CITYROAD,        ALL    }    [TestClass()]    public class EnumHelperTests    {        [TestMethod()]        public void CheckedContainEnumNameTest()        {            Assert.IsTrue(EnumHelper.CheckedContainEnumName<AreaMode>("none"));        }        [TestMethod()]        public void GetDescriptionTest()        {            Assert.AreEqual("NONE", AreaMode.NONE.GetDescription());        }        [TestMethod()]        public void ParseEnumDescriptionTest()        {            Assert.AreEqual(AreaMode.NONE, EnumHelper.ParseEnumDescription<AreaMode>("NONE", AreaMode.CITYTOWN));        }        [TestMethod()]        public void ParseEnumNameTest()        {            Assert.AreEqual(AreaMode.ALL, EnumHelper.ParseEnumName<AreaMode>("ALL"));        }    }}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.