Esempio n. 1
0
        /// <summary>
        /// Gets a string value for a particular enum value.
        /// </summary>
        /// <param name="value">Value.</param>
        /// <returns>String Value associated via a <see cref="EnumAttribute"/> attribute, or null if not found.</returns>
        public static string GetStringValue(Enum value)
        {
            string output = null;
            Type   type   = value.GetType();

            if (StringValues.ContainsKey(value))
            {
                EnumAttribute enumAttribute = StringValues[value] as EnumAttribute;
                if (enumAttribute != null)
                {
                    output = enumAttribute.Value;
                }
            }
            else
            {
                //Look for our 'StringValueAttribute' in the field's custom attributes
                FieldInfo       fi    = type.GetField(value.ToString());
                EnumAttribute[] attrs =
                    fi.GetCustomAttributes(typeof(EnumAttribute), false) as EnumAttribute[];
                if (attrs != null && attrs.Length > 0)
                {
                    StringValues.Add(value, attrs[0]);
                    output = attrs[0].Value;
                }
            }
            return(output);
        }
Esempio n. 2
0
        public bool ContainsKey(string value)
        {
            bool          result = false;
            EnumAttribute sva    = new EnumAttribute(value);

            if (EnumValues.ContainsKey(sva))
            {
                result = true;
            }
            return(result);
        }
Esempio n. 3
0
        public Enum GetEnum(string value)
        {
            EnumAttribute sva = new EnumAttribute(value);

            return(EnumValues[sva]);
        }