コード例 #1
0
        static void Main(string[] args)
        {
            Sex sex1 = Sex.Man;
            Sex sex2 = Sex.WoMan;

            //获取枚举的value
            Console.WriteLine((int)sex1); //0
            Console.WriteLine((int)sex2); //1

            //获取枚举的name (下面三种方式都可以)
            Console.WriteLine(sex1.ToString());                    //Man
            Console.WriteLine(Enum.GetName(typeof(Sex), sex1));    //Man
            Console.WriteLine(Enum.GetName(sex2.GetType(), sex2)); //WoMan

            //枚举中一般使用Description特性值用于页面显
            Console.WriteLine(sex1.GetDescription());
            Console.ReadLine();
        }
コード例 #2
0
ファイル: Sex.cs プロジェクト: Ari100kratov/TestManagerByAri
        /// <summary>
        /// Возвращает пол сотрудника на русском языке
        /// </summary>
        /// <param name="sex">Перечисление пола сотрудника</param>
        /// <returns></returns>
        public static string GetSexResource(Sex sex)
        {
            var type       = sex.GetType();
            var memberInfo = type.GetMember(sex.ToString());

            //Проверяем существует ли такой элемент в перечислении
            if (memberInfo == null || memberInfo.Length <= 0)
            {
                return(sex.ToString());
            }

            var attributes = memberInfo[0].GetCustomAttributes(typeof(DescriptionAttribute), false);

            //Проверяем существует ли атрибут описания
            if (attributes == null || attributes.Length <= 0)
            {
                return(sex.ToString());
            }

            return(((DescriptionAttribute)attributes[0]).Description);
        }