关于C#的Enum(枚举),以前用过,老觉得有很多的坑。如果Enum中有2个值相同,则会出现一些问题:

1 , enum Platform : int

        EnumControl

using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace EnumTest.ainy{    ///     /// 名字 = 值    ///    public enum Platform : int{        Apple = 1,        Android = 2,        Windows = 3,        MACOS = 4,        WP = 3 // 此值与Windows的相同 , 在很多情况下只能取到“Windows” , 所以尽量不要取的相同    }    ///     /// 对枚举类型的操作测试    ///     public class EnumControl    {        private static  EnumControl instance;        public static EnumControl Instance        {            get {                if (EnumControl.instance == null) {                    EnumControl.instance = new EnumControl();                }                return EnumControl.instance                ;}            private set            {                EnumControl.instance = value            ;} }        private readonly Type enumType;        public EnumControl()        {            this.enumType = typeof(Platform);        }        #region        ///         /// 通过值找到键值        /// 《!important》:如value有多个,给出第一个        /// 例如 : 如参数是3 , 则返回 Windows 而非WP        ///         /// 
值        /// 
        public string GetEnumKeyByValue(int i)         {            return Enum.GetName(this.enumType, i);        }        /// 
        /// 返回所有的键值        ///         /// 
        public string[] GetEnumKeys()        {            return Enum.GetNames(this.enumType);        }        #endregion        #region        /// 
        /// 可以是键值可以是值 ,《!importan》 有重复的话 , 返回第一个        ///         /// 
WP/3        /// 
        public string GetEnumNameKeyByKeyOrValue(string key) {            return Enum.Parse(this.enumType, key).ToString();        }        public int GetEnumValue(string key)        {            return Convert.ToInt32( Enum.Parse(this.enumType , key));        }        /// 
        /// 得到所有的        /// keys        ///         /// 
        public Array GetEnumValues()        {            return Enum.GetValues(this.enumType);        }        #endregion        public Type GetEnumType()        {            Type t = Enum.GetUnderlyingType(this.enumType);            return t;        }        public string getChineseName(Platform myEnum) {            string bcStr = string.Empty;            switch (myEnum) {                 case Platform.Apple:                    bcStr = "苹果";                    break;                case Platform.Android:                    bcStr = "安卓";                    break;                case Platform.Windows:                    bcStr = "微软";                    break;                case Platform.MACOS:                    bcStr = "苹果PC";                    break;                //case Platform.WP:                //    bcStr = "Window Phone";                //    break;                default:                    break;            }            return bcStr;        }        public int getValus(Platform myEnum)        {            int bcInt = (int)myEnum;            return bcInt;        }    }}

测试:

using System;using System.Collections.Generic;using System.Linq;using System.Text;using EnumTest.ainy;namespace EnumTest{    class Program    {        static void Main(string[] args)        {            Console.WriteLine("----{0}", EnumControl.Instance.GetEnumKeyByValue(3));            Console.WriteLine("_________________________________________________");            foreach (var a in EnumControl.Instance.GetEnumKeys()) {                Console.WriteLine("Array --- {0}", a);            }            Console.WriteLine("_________________________________________________");            Console.WriteLine("4-----{0}", EnumControl.Instance.GetEnumNameKeyByKeyOrValue("4"));            Console.WriteLine("3-----{0}", EnumControl.Instance.GetEnumNameKeyByKeyOrValue("3"));            Console.WriteLine("5-----{0}", EnumControl.Instance.GetEnumNameKeyByKeyOrValue("5"));            Console.WriteLine("WP-----{0}", EnumControl.Instance.GetEnumNameKeyByKeyOrValue("WP"));            Console.WriteLine("_________________________________________________");            Console.WriteLine("Apple-----{0}", EnumControl.Instance.GetEnumValue("Apple"));            //没有意义            //Console.WriteLine("-----{0}", EnumControl.Instance.GetEnumValue("2"));            Console.WriteLine("_________________________________________________");            foreach (var a in EnumControl.Instance.GetEnumValues()) {                Console.WriteLine("Array --- {0}", a);            }            Console.WriteLine("_________________________________________________");            Console.WriteLine("BaseType-----{0}", EnumControl.Instance.GetEnumType());            Console.WriteLine("_________________________________________________");            Console.WriteLine("ChineseName-----{0}", EnumControl.Instance.getChineseName( Platform.WP ));            Console.WriteLine("_________________________________________________");            Console.WriteLine(" Platform.Windows Value-----{0}", EnumControl.Instance.getValus(Platform.Windows));            Console.WriteLine(" Platform.WP Value-----{0}", EnumControl.Instance.getValus(Platform.WP));            Console.ReadKey();        }    }}

结果:

因为:Windows和WP的值都为3 , 有的时候只能取到Windows  (通过 3 或者 WP找的时候)

看这个,根本就编辑不过: