[Reprint] [C #] enumeration operation (obtain Description from enumeration, obtain enumeration Based on Description, and convert enumeration to ArrayList) tool class,

Source: Internet
Author: User

[Reprint] [C #] enumeration operation (obtain Description from enumeration, obtain enumeration Based on Description, and convert enumeration to ArrayList) tool class,

Key code:

Using System; using System. collections; using System. collections. generic; using System. componentModel; using System. reflection; namespace CSharpUtilHelpV2 {/// <summary> /// based on. NET 2.0 Enumeration Tool class /// </summary> public static class EnumToolV2 {// <summary> /// obtain the Description from the enumeration /// Description: /// unit test --> pass /// </summary> /// <param name = "enumName"> obtain enumeration descriptions </param> /// <returns> description </returns> public static strin G 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> /// obtain the field 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> /// obtain the enumeration according to Description /// Description: /// unit test --> pass /// </summary> /// <typeparam name = "T"> Enumeration type </typeparam> /// <param name =" description "> enumeration description </param> // <returns> enumeration </returns> public static T GetEnumName <T> (string description) {Type _ type = typeof (T); foreach (FieldI Nfo 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} cannot find the corresponding enumeration. ", description)," Description ") ;}/// <summary> /// convert enumeration to ArrayList /// description: /// if it is not an enumeration type, NULL // unit test --> pass /// </summary> /// <param name = "type"> Enumeration 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 ;}}}

Unit test code:

Using Microsoft. visual Studio. testTools. unitTesting; using System. collections; using System. collections. generic; namespace CSharpUtilHelpV2.Test {public enum TestEnum {[System. componentModel. description ("first")] One, [System. componentModel. description ("second")] Two, [System. componentModel. description ("third")] Three, [System. componentModel. description ("Fifth")] Five, [System. componentModel. description ("All")] All} [TestClass ()] public class EnumToolV2Test {[TestMethod ()] public void GetDescriptionTest () {string _ actual = TestEnum. five. getDescription (); string _ expected = "Fifth"; Assert. areEqual (_ expected, _ actual);} [TestMethod ()] public void GetEnumNameTest () {TestEnum _ actual = EnumToolV2.GetEnumName <TestEnum> ("Fifth "); 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, "first"); _ expected. add (new KeyValuePair <Enum, string> (TestEnum. two, "second"); _ expected. add (new KeyValuePair <Enum, string> (TestEnum. three, "third"); _ expected. add (new KeyValuePair <Enum, string> (TestEnum. five, "Fifth"); _ expected. add (new KeyValuePair <Enum, string> (TestEnum. all, "All"); CollectionAssert. areEqual (_ expected, _ actual) ;}} unit test results:
  

Related Article

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.