segunda-feira, 20 de julho de 2015

Enumerador

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI.WebControls;
using System.Reflection;
using System.ComponentModel;

namespace Lapuinka.Utils
{
    ///
    /// Classe Enumerador.
    ///
    public static class Enumerador
    {      
        ///
        /// Carrega um DropDownList a partir de um Enumerador.
        ///
        ///
        ///
        public static void LoadDdlEnum(DropDownList ddl)
        {
            var objectType = typeof(T);
            var properties = objectType.GetFields();

            foreach (var fieldObject in properties)
            {
                var attributes =
                    (DescriptionAttribute[])fieldObject.GetCustomAttributes(typeof(DescriptionAttribute), false);

                if (attributes.Length <= 0) continue;

                ddl.Items.Add(new ListItem(attributes[0].Description, fieldObject.GetRawConstantValue().ToString()));
            }
        }
        ///
        /// Carrega um DropDownList a partir de um Enumerador.
        ///
        ///
        ///
        public static void LoadDdlEnum(ListBox ddl)
        {
            var objectType = typeof(T);
            var properties = objectType.GetFields();

            foreach (var fieldObject in properties)
            {
                var attributes =
                    (DescriptionAttribute[])fieldObject.GetCustomAttributes(typeof(DescriptionAttribute), false);

                if (attributes.Length <= 0) continue;

                ddl.Items.Add(new ListItem(attributes[0].Description, fieldObject.GetRawConstantValue().ToString()));
            }
        }
        ///
        /// Carregha um RadioButton a partir de um Enumerador.
        ///
        ///
        ///
        public static void LoadRbEnum(RadioButtonList rb)
        {
            var objectType = typeof(T);
            var properties = objectType.GetFields();

            foreach (var fieldObject in properties)
            {
                var attributes =
                    (DescriptionAttribute[])fieldObject.GetCustomAttributes(typeof(DescriptionAttribute), false);

                if (attributes.Length <= 0) continue;

                rb.Items.Add(new ListItem(attributes[0].Description, fieldObject.GetRawConstantValue().ToString()));
            }
        }

        ///
        /// Método GetEnumDescription.
        ///
        /// T
        /// Valor.
        ///
        public static string GetEnumDescription(string enumValue)
        {
            var objectType = typeof(T);
            var properties = objectType.GetFields();

            foreach (var fieldObject in properties)
            {
                if (enumValue == fieldObject.Name)
                {
                    var attributes =
                        (DescriptionAttribute[])fieldObject.GetCustomAttributes(typeof(DescriptionAttribute), false);

                    if (attributes.Length <= 0) continue;

                    return attributes[0].Description;
                }
            }

            return "";
        }
    }
}



Postar um comentário