Ejemplo n.º 1
0
        public static string GetName(this Enum enuType)
        {
            string strName = string.Empty;

            EnumAttribute objAttribute = GetAttribute(enuType);

            if (objAttribute != null)
            {
                strName = objAttribute.Name;
            }

            return(strName);
        }
Ejemplo n.º 2
0
        public static string GetDescription(this Enum enuType)
        {
            string strDescription = string.Empty;

            EnumAttribute objAttribute = GetAttribute(enuType);

            if (objAttribute != null)
            {
                strDescription = objAttribute.Description;
            }

            return(strDescription);
        }
Ejemplo n.º 3
0
        public static EnumAttribute GetAttribute(this Enum enuType)
        {
            EnumAttribute objAttribute = null;

            MemberInfo[] objMemberInfo = enuType.GetType().GetMember(enuType.ToString());
            if ((objMemberInfo != null) && (objMemberInfo.Length > 0))
            {
                object[] objAttributes = objMemberInfo[0].GetCustomAttributes(typeof(EnumAttribute), true);
                if ((objAttributes != null) && (objAttributes.Length > 0))
                {
                    objAttribute = objAttributes[0] as EnumAttribute;
                }
            }

            return(objAttribute);
        }
Ejemplo n.º 4
0
        public static TEnumType GetByCode <TEnumType>(string strCode)
            where TEnumType : struct, IConvertible
        {
            if (typeof(TEnumType).IsEnum == false)
            {
                throw new ArgumentException("The generic TSourceEnumType must be an Enum.", "enuValue");
            }

            TEnumType enuReturnValue = default(TEnumType);

            foreach (TEnumType enuValue in Enum.GetValues(typeof(TEnumType)))
            {
                EnumAttribute objAttribute = ((Enum)(object)enuValue).GetAttribute();
                if ((objAttribute != null) && (objAttribute.Code == strCode))
                {
                    enuReturnValue = enuValue;
                    break;
                }
            }

            return(enuReturnValue);
        }