[C #] Enumhelper

Source: Internet
Author: User

usingSystem;usingSystem.ComponentModel;usingSystem.Reflection;namespacelh.dotnet2.utilities.common{// <summary>    // Enumerate helper classes    // </summary>     Public Static classEnumhelper {#regionPrivate methodsPrivate StaticDescriptionattribute[] Getdescriptattr ( ThisFieldInfo FieldInfo) {if(FieldInfo! =NULL)            {return(descriptionattribute[]) fieldinfo.getcustomattributes (typeof(DescriptionAttribute),false); }return NULL; }#endregionPrivate methods// <summary>        /// Determine if enumeration includes enumeration constant names        // </summary>        // <typeparam name= "T" > Enumeration </typeparam>        // <param name= "Enumname" > Enumeration constant name </param>        // <returns> whether to include enumeration constant name </returns>        //Founder: Yan Zhi Wei        ///Creation time: 2015-06-04 17:17        /// remarks description:<c>null</c>         Public Static BOOLCheckedcontainenumname<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>        /// Get description from the enumeration        // </summary>        /// <param name= "Targetenum" > Need to get enumeration description of enumerations </param>        /// <returns> description content </returns>         Public Static stringGetDescription ( 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>        /// Get enumeration based on description        // </summary>        // <typeparam name= "T" > Enum type </typeparam>        /// <param name= "description" > Enumeration description </param>        /// <returns> Enumeration </returns>         Public StaticT parseenumdescription<t> ( This stringDescription, T defaultvalue)whereT:struct, IConvertible {if(typeof(T). Isenum) {Type _type =typeof(T);foreach(FieldInfo Fieldinch_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>        /// Convert enumeration constant names to enumerations        // </summary>        // <typeparam name= "T" > Enumeration </typeparam>        // <param name= "Enumname" > Enumeration constant name </param>        // <returns></returns>        //Founder: Yan Zhi Wei        ///Creation time: 2015-06-05 9:16        /// remarks description:<c>null</c>         Public StaticT parseenumname<t> ( This stringEnumname)whereT:struct, IConvertible {returnT Enum.parse (typeof(T), Enumname,true); }    }}

Unit tests:

usingMicrosoft.VisualStudio.TestTools.UnitTesting;namespaceyanzhiwei.dotnet2.utilities.common.tests{ Public enumAreamode {NONE, city, town, ROAD, Citytown, Townroad, Cityroad, All} [TestClass ()] Public classenumhelpertests {[TestMethod ()] Public voidCheckedcontainenumnametest () {Assert.istrue (enumhelper.checkedcontainenumname<areamode> ("None")); } [TestMethod ()] Public voidGetdescriptiontest () {assert.areequal ("NONE", AreaMode.NONE.GetDescription ()); } [TestMethod ()] Public voidParseenumdescriptiontest () {assert.areequal (Areamode.none, enumhelper.parseenumdescription<areamode& gt; ("NONE", Areamode.citytown)); } [TestMethod ()] Public voidParseenumnametest () {assert.areequal (Areamode.all, enumhelper.parseenumname<areamode> ("All")); }    }}

[C #] Enumhelper

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.