예제 #1
0
        /// <summary>
        /// The obter data source.
        /// </summary>
        /// <param name="enumType">
        /// The enum Type.
        /// </param>
        /// <returns>
        /// Retorna o enum formatada em uma lista tipada. <see cref="List"/>.
        /// </returns>
        public static List <AtributoEnum> ListarEnums(this Type enumType)
        {
            var enuns = new List <AtributoEnum>();

            foreach (Enum val in enumType.GetEnumValues())
            {
                FieldInfo fi = enumType.GetField(val.ToString());

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

                if (attributes.Any())
                {
                    AtributoEnum attr = attributes[0];

                    enuns.Add(new AtributoEnum(attr.Valor, attr.Descricao, attr.Exibir, attr.Tipo));
                }
            }

            return(enuns);
        }
예제 #2
0
        /// <summary>
        /// The get enum description.
        /// </summary>
        /// <param name="valor">
        /// The value.
        /// </param>
        /// <typeparam name="TEnum"> Tipo de enum a ser atribuido.
        /// </typeparam>
        /// <returns>
        /// The <see cref="string"/>.
        /// </returns>
        public static TEnum ObterEnumPorValor <TEnum>(string valor)
        {
            Type enumType = typeof(TEnum);

            TEnum enumObjectType = default(TEnum);

            foreach (Enum val in enumType.GetEnumValues())
            {
                FieldInfo fi = enumType.GetField(val.ToString());

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

                AtributoEnum attr = attributes[0];

                if (attr.Valor.ToUpper() == valor.ToUpper())
                {
                    enumObjectType = (TEnum)Enum.Parse(typeof(TEnum), val.ToString());

                    break;
                }
            }

            return(enumObjectType);
        }