quarta-feira, 18 de julho de 2012

Convert Enum to Tuple List...

Simple code to Convert Enum to a Tuple List...



        public static List> ToList(Type myEnum)
        {
            List> list= new List>();
            var names = Enum.GetNames(myEnum);
            for (int i = 0; i <= names.GetUpperBound(0); i++)
            {
                   list.Add(new Tuple(Enum.Parse(myEnum, names[i]).ToString(),
                                    GetEnumDescription((Enum)Enum.Parse(myEnum, names[i]))));
            }
            return list;
        }
        public static String GetEnumDescription(Enum value)
        {
            FieldInfo fi = value.GetType().GetField(value.ToString());
            var attributes = (DescriptionAttribute[])fi.GetCustomAttributes(
                    typeof(DescriptionAttribute), false);

            return (attributes.Length > 0) ? attributes[0].Description : value.ToString();
        }
Postar um comentário