/// <summary> /// Returns the description of the specified enum. /// </summary> /// <param name="value">The value of the enum for which to return the description.</param> /// <returns>A description of the enum, or the enum name if no description exists.</returns> public static string GetEnumDescription(object value) { if (value == null) { return(null); } Type type = value.GetType(); //Make sure the object is an enum. if (!type.IsEnum) { throw new ApplicationException("Value parameter must be an enum."); } FieldInfo fieldInfo = type.GetField(value.ToString()); object[] descriptionAttributes = fieldInfo.GetCustomAttributes(typeof(DescriptionAttribute), false); //If no DescriptionAttribute exists for this enum value, check the DescriptiveEnumEnforcementAttribute and decide how to proceed. if (descriptionAttributes == null || descriptionAttributes.Length == 0) { object[] enforcementAttributes = fieldInfo.GetCustomAttributes(typeof(DescriptiveEnumEnforcementAttribute), false); //If a DescriptiveEnumEnforcementAttribute exists, either throw an exception or return the name of the enum instead. if (enforcementAttributes != null && enforcementAttributes.Length == 1) { DescriptiveEnumEnforcementAttribute enforcementAttribute = (DescriptiveEnumEnforcementAttribute)enforcementAttributes[0]; if (enforcementAttribute.EnforcementType == DescriptiveEnumEnforcementType.ThrowException) { throw new ApplicationException("No Description attributes exist in enforced enum of type '" + type.Name + "', value '" + value.ToString() + "'."); } return(value.ToString()); } else //Just return the name of the enum. { return(value.ToString()); } } else if (descriptionAttributes.Length > 1) { throw new ApplicationException("Too many Description attributes exist in enum of type '" + type.Name + "', value '" + value.ToString() + "'."); } //Return the value of the DescriptionAttribute. return(descriptionAttributes[0].ToString()); }
public static string GetEnumName(object value) { if (value == null) { return(null); } Type type = value.GetType(); if (!type.IsEnum) { throw new ApplicationException("Value parameter must be an enum."); } FieldInfo fieldInfo = type.GetField(value.ToString()); object[] nameAttributes = fieldInfo.GetCustomAttributes(typeof(NameAttribute), false); if (nameAttributes == null || nameAttributes.Length == 0) { object[] enforcementAttributes = fieldInfo.GetCustomAttributes(typeof(DescriptiveEnumEnforcementAttribute), false); if (enforcementAttributes != null && enforcementAttributes.Length == 1) { DescriptiveEnumEnforcementAttribute enforcementAttribute = (DescriptiveEnumEnforcementAttribute)enforcementAttributes[0]; if (enforcementAttribute.EnforcementType == DescriptiveEnumEnforcementAttribute.EnforcementTypeEnum.ThrowException) { throw new ApplicationException("No Name attributes exist in enforced enum of type '" + type.Name + "', value '" + value.ToString() + "'."); } return(EnumValueToDisplayString(value.ToString())); } else { return(EnumValueToDisplayString(value.ToString())); } } else if (nameAttributes.Length > 1) { throw new ApplicationException("Too many Name attributes exist in enum of type '" + type.Name + "', value '" + value.ToString() + "'."); } return(nameAttributes[0].ToString()); }