My enum consists of the following values:
private enum PublishStatusses{
NotCompleted,
Completed,
Error
};
I want to be able to
This is an update to Ray Booysen's code that uses the generic GetCustomAttributes method and LINQ to make things a bit tidier.
///
/// Gets the value of the on an struct, including enums.
///
/// The type of the struct.
/// A value of type
/// If the struct has a Description attribute, this method returns the description. Otherwise it just calls ToString() on the struct.
/// Based on http://stackoverflow.com/questions/479410/enum-tostring/479417#479417, but useful for any struct.
public static string GetDescription(this T enumerationValue) where T : struct
{
return enumerationValue.GetType().GetMember(enumerationValue.ToString())
.SelectMany(mi => mi.GetCustomAttributes(false),
(mi, ca) => ca.Description)
.FirstOrDefault() ?? enumerationValue.ToString();
}