Пример #1
0
        /// <summary>
        /// Retourne l'enum selon la valeur en string fournie
        /// </summary>
        /// <param name="value">La valeur</param>
        /// <returns>L'enum</returns>
        public static T GetEnumTypeFromDescription <T>(string value)
        {
            T    enumValue = default(T);
            bool found     = false;

            foreach (T type in EnumFunction.EnumToList <T>())
            {
                string    description = string.Empty;
                FieldInfo fi          = type.GetType().GetField(type.ToString());

                DescriptionAttribute[] attributes = (DescriptionAttribute[])fi.GetCustomAttributes(typeof(DescriptionAttribute), false);

                if (attributes != null && attributes.Length > 0)
                {
                    if (attributes[0].Description.Equals(value))
                    {
                        enumValue = type;
                        found     = true;
                    }
                }
            }

            if (found)
            {
                return(enumValue);
            }
            else
            {
                throw new Exception(string.Format(MKS.Library.Ressources.ErrorMessages.DESIGN_ERROR_201, value, typeof(T).ToString()));
            }
        }
Пример #2
0
        /// <summary>
        /// Cette fonction permet d'obtenir les descriptions de l'enum sous forme de liste de string
        /// </summary>
        /// <typeparam name="T">L'Enum à énumérer</typeparam>
        /// <returns>Les descriptions de l'enum</returns>
        public static List <string> GetEnumDescriptionList <T>()
        {
            List <string> lst = new List <string>();

            foreach (T type in EnumFunction.EnumToList <T>())
            {
                string    description = string.Empty;
                FieldInfo fi          = type.GetType().GetField(type.ToString());

                DescriptionAttribute[] attributes = (DescriptionAttribute[])fi.GetCustomAttributes(typeof(DescriptionAttribute), false);

                if (attributes != null && attributes.Length > 0)
                {
                    description = attributes[0].Description;
                }
                else
                {
                    description = type.ToString();
                }

                lst.Add(description);
            }
            return(lst);
        }