Chủ Nhật, 29 tháng 9, 2019

Enum trong C#

A. Lớp tập hợp các Enum:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
 
namespace KieuEnum
{
    class EnumCollection
    {
        public enum WeekDay
        {
            MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY
        }
 
        //[Flags]
        public enum WeekDayDescription
        {
            [Description("Thứ hai")]
            MONDAY,
            [Description("Thứ ba")]
            TUESDAY,
            [Description("Thứ tư")]
            WEDNESDAY,
            [Description("Thứ năm")]
            THURSDAY,
 
            [Description("Thứ sáu")]
            FRIDAY,
            [Description("Thứ bảy")]
            SATURDAY,
            [Description("Chủ nhật")]
            SUNDAY,
 
        }
        public enum GioiTinh
        {
            [Description("Nam")]
            NAM = 1,
            [Description("Nữ")]
            NU = 0
        }
 
        //Hint: Change the method signature and input paramter to use the type parameter T
        public static string GetEnumDescription(Enum GenericEnum) 
        {
            Type genericEnumType = GenericEnum.GetType();
            MemberInfo[] memberInfo = genericEnumType.GetMember(GenericEnum.ToString());
            if ((memberInfo != null && memberInfo.Length > 0))
            {
                var _Attribs = memberInfo[0].GetCustomAttributes(typeof(System.ComponentModel.DescriptionAttribute), false);
                if ((_Attribs != null && _Attribs.Count() > 0))
                {
                    return ((System.ComponentModel.DescriptionAttribute)_Attribs.ElementAt(0)).Description;
                }
            }
            return GenericEnum.ToString();
        }
 
        public static bool IsGioiTinhNam(GioiTinh gt)
        {
            return Convert.ToBoolean(gt);
        }
    }
}

B. Hàm main:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
 
namespace KieuEnum
{
    class Program
    {
        //public static List<T> GetFlagEnumAttributes<T>(this Enum flagEnum) where T : Attribute
        //{
        //    var type = flagEnum.GetType();
        //    return Enum.GetValues(type)
        //       .Cast<Enum>()
        //       .Where(flagEnum.HasFlag)
        //       .Select(e => type.GetMember(e.ToString()).First())
        //       .Select(info => info.GetCustomAttribute<T>())
        //       .Where(attribute => attribute != null)
        //       .ToList();
        //}
        static int Compare<T>(T first, T second)
        {
            if (first.Equals(second))
            {
                return 0;
            }
 
            return 1;
        }
        static void Main(string[] args)
        {
 
 
            Console.OutputEncoding = Encoding.UTF8;
 
 
            Console.WriteLine(getJob(EnumCollection.WeekDay.MONDAY));
 
 
            int result1 = Compare(2, 2);
            int result2 = Compare("abc""def");
 
            Console.WriteLine(result1);
            Console.WriteLine(result2);
 
            string description = EnumCollection.GetEnumDescription(EnumCollection.WeekDayDescription.THURSDAY);
            Console.WriteLine(description + ":" + EnumCollection.WeekDayDescription.THURSDAY + ":" + (int)EnumCollection.WeekDayDescription.THURSDAY);
 
            EnumCollection.GioiTinh nu = EnumCollection.GioiTinh.NU;
            Console.WriteLine(EnumCollection.GetEnumDescription(nu) + ":" + nu + ":" + (int)nu);
 
 
            Console.WriteLine(EnumCollection.IsGioiTinhNam(nu) ? "Nam" : "Nu");
 
 
            Console.ReadKey();
        }
 
        public static String getJob(EnumCollection.WeekDay weekDay)
        {
            if (weekDay == EnumCollection.WeekDay.SATURDAY || weekDay == EnumCollection.WeekDay.SUNDAY)
            {
                return "Nothing";
            }
            return "Coding for " + weekDay.ToString();
        }
    }
}

Không có nhận xét nào:

Đăng nhận xét