Beispiel #1
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="enumValue"></param>
        /// <returns></returns>
        static public EnumTextAttribute GetEnumTextAttribute(object enumValue)
        {
            if (enumValue == null)
            {
                throw new ArgumentNullException("enumValue");
            }

            Type type = enumValue.GetType();

            if (!type.IsEnum)
            {
                throw new ArgumentException(
                          string.Format("Type '{0}' is not enum",
                                        enumValue.GetType().Name));
            }

            FieldInfo fi = type.GetField(enumValue.ToString());

            if (fi == null)
            {
                return(null);
            }

            object[] atts = fi.GetCustomAttributes(typeof(EnumTextAttribute), false);
            if (atts.Length == 0)
            {
                return(null);
            }

            EnumTextAttribute etAtt = (EnumTextAttribute)atts[0];

            return(etAtt);
        }
Beispiel #2
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="enumValue"></param>
        /// <returns></returns>
        static public string GetEnumTextAttributeValue(object enumValue)
        {
            EnumTextAttribute etAtt = GetEnumTextAttribute(enumValue);

            if (etAtt == null)
            {
                string s = string.Format("{0}.{1} has not EnumTextAttribute",
                                         enumValue.GetType().Name,
                                         enumValue.ToString());
                throw new ArgumentException(s);
            }
            return(etAtt.Text);
        }