private string GetDescription(OperationType enumerationValue)
        {
            Type type = enumerationValue.GetType();

            if (!type.IsEnum)
            {
                throw new ArgumentException("EnumerationValue must be of Enum type", "enumerationValue");
            }

            //Tries to find a DescriptionAttribute for a potential friendly name
            //for the enum
            MemberInfo[] memberInfo = type.GetMember(enumerationValue.ToString());
            if (memberInfo != null && memberInfo.Length > 0)
            {
                object[] attrs = memberInfo[0].GetCustomAttributes(typeof(DescriptionAttribute), false);

                if (attrs != null && attrs.Length > 0)
                {
                    //Pull out the description value
                    return(((DescriptionAttribute)attrs[0]).Description);
                }
            }
            //If we have no description attribute, just return the ToString of the enum
            return(enumerationValue.ToString());
        }
Example #2
0
        public static string GetLabel(this OperationType e)
        {
            Type  type   = e.GetType();
            Array values = Enum.GetValues(type);

            foreach (int val in values)
            {
                if ((OperationType)val == e)
                {
                    System.Reflection.MemberInfo[] memberInfo           = type.GetMember(type.GetEnumName(val));
                    DescriptionAttribute           descriptionAttribute = memberInfo[0]
                                                                          .GetCustomAttributes(typeof(DescriptionAttribute), false)
                                                                          .FirstOrDefault() as DescriptionAttribute;

                    if (descriptionAttribute != null)
                    {
                        return(descriptionAttribute.Description);
                    }
                }
            }

            return(null);
        }