程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#入門知識 >> 把數據庫中有關枚舉項值的數字字符串轉換成文字字符串,枚舉成文

把數據庫中有關枚舉項值的數字字符串轉換成文字字符串,枚舉成文

編輯:C#入門知識

把數據庫中有關枚舉項值的數字字符串轉換成文字字符串,枚舉成文


 

標題可能無法表達我的本意。比如,有這樣一個枚舉:

 

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

 

數據庫中,某表某字段保存值為"0,1,2",在顯示的時候,我們希望是"第一個選擇,第二個選擇,第三個選擇"。如何做呢?

 

可以為枚舉項上面標注自定義特性。先自定義一個特性如下:

 

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

 

然後,把自定義特性標注放到枚舉項上去。

 

    public enum MyChoice
    {
        [EnumDisplayName("我的第一個選擇")]
        MyFirstChoice = 0,
        [EnumDisplayName("我的第二個選擇")]
        MySecondChoice =1,
        [EnumDisplayName("我的第三個選擇")]
        MyThirdChoice = 2
    }  

 

現在,需要一個幫助方法,能讀出枚舉項上的自定義特性EnumDisplayName。

 

   public class EnumExt
    {
        /// <summary>
        /// 獲取枚舉項的注釋
        /// </summary>
        /// <param name="e">枚舉項</param>
        /// <returns></returns>
        public static string GetEnumDescription(object e)
        {
            //獲取枚舉項
            Type t = e.GetType();
            //獲取枚舉項的字段
            FieldInfo[] fis = t.GetFields();
            foreach (FieldInfo fi in fis)
            {
                //如果當前字段名稱不是當前枚舉項
                if (fi.Name != e.ToString())
                {
                    continue;//結束本次循環
                }
                //如果當前字段的包含自定義特性
                if (fi.IsDefined(typeof (EnumDisplayNameAttribute), true))
                {
                    //獲取自定義特性的屬性值
                    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 = "--請選擇--",Value = ""});
            foreach (object e in Enum.GetValues(enumType))
            {
                selectList.Add(new SelectListItem { Text = GetEnumDescription(e), Value = ((int)e).ToString() });
            }
            return selectList;
        }
    }   

 

以上,

● GetEnumDescription方法根據枚舉項獲取其上的自定義特性EnumDisplayNameAttribute的DisplayName屬性值。

● GetSelectList方法根據枚舉的Type類型返回SelectListItem集合,通常在ASP.NET MVC中使用。

 

最後,就能實現本篇的需求:

 

        static void Main(string[] args)
        {
            string myChoiceInt = "0,1,2";
            string[] choiceArr = myChoiceInt.Split(',');
            string temp = string.Empty;
            foreach (string item in choiceArr)
            {
                //轉換成枚舉的類型
                short enumValShort = short.Parse(item);
                temp = temp + EnumExt.GetEnumDescription((MyChoice)enumValShort) + ",";
            }
            Console.WriteLine(temp.Substring(0, temp.Length - 1));
            Console.ReadKey();
        }

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved