Converts the numeric string related to the enumerated item value in the database into a text string and enumerates the data

Source: Internet
Author: User

Converts the numeric string related to the enumerated item value in the database into a text string and enumerates the data

 

The title may not express my intention. For example, there is such an enumeration:

 

    public enum MyChoice
    {
        MyFirstChoice = 0,
        MySecondChoice =1,
        MyThirdChoice = 2
    }

 

In the database, the value of a field in a table is "0, 1, 2". When displayed, we want to "select first, select second, and select third ". What should we do?

 

You can add custom features to the enumeration items. First, define a feature as follows:

 

    public class EnumDisplayNameAttribute : Attribute
    {
        private string _displayName;
        public EnumDisplayNameAttribute(string displayName)
        {
            _displayName = displayName;
        }
        public string DisplayName
        {
            get
            {
                return _displayName;
            }
        }
    }

 

Then, add the custom feature annotation to the enumeration items.

 

    public enum MyChoice
    {
[EnumDisplayName ("My first choice")]
        MyFirstChoice = 0,
[EnumDisplayName ("my second choice")]
        MySecondChoice =1,
[EnumDisplayName ("My third choice")]
        MyThirdChoice = 2
    }  

Now, you need a help method to read the custom EnumDisplayName on the enumeration item.

 

   public class EnumExt
    {
        /// <summary>
/// Obtain the annotation of the enumerated items
        /// </summary>
/// <Param name = "e"> enumeration item </param>
        /// <returns></returns>
        public static string GetEnumDescription(object e)
        {
// Obtain the enumerated items
            Type t = e.GetType();
// Obtain the fields of the enumerated items
            FieldInfo[] fis = t.GetFields();
            foreach (FieldInfo fi in fis)
            {
// If the current field name is not the current enumerated item
                if (fi.Name != e.ToString())
                {
Continue; // end this loop
                }
// If the current field contains custom features
                if (fi.IsDefined(typeof (EnumDisplayNameAttribute), true))
                {
// Obtain the property value of the custom feature
                    return (fi.GetCustomAttributes(typeof(EnumDisplayNameAttribute), true)[0] as EnumDisplayNameAttribute).DisplayName;
                }
            }
            return e.ToString();
        }
        public static List<SelectListItem> GetSelectList(Type enumType)
        {
            List<SelectListItem> selectList = new List<SelectListItem>();
// SelectList. Add (new SelectListItem {Text = "-- select --", Value = ""});
            foreach (object e in Enum.GetValues(enumType))
            {
                selectList.Add(new SelectListItem { Text = GetEnumDescription(e), Value = ((int)e).ToString() });
            }
            return selectList;
        }
    }   

 

Above,

● The GetEnumDescription method obtains the DisplayName attribute value of the custom EnumDisplayNameAttribute Based on the enumerated items.

● The GetSelectList method returns the SelectListItem set based on the enumerated Type, which is usually used in ASP. net mvc.

 

Finally, we can meet the requirements of this article:

 

        static void Main(string[] args)
        {
            string myChoiceInt = "0,1,2";
            string[] choiceArr = myChoiceInt.Split(',');
            string temp = string.Empty;
            foreach (string item in choiceArr)
            {
// Convert to the enumerated type
                short enumValShort = short.Parse(item);
                temp = temp + EnumExt.GetEnumDescription((MyChoice)enumValShort) + ",";
            }
            Console.WriteLine(temp.Substring(0, temp.Length - 1));
            Console.ReadKey();
        }

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.