add support for `System.ComponentModel.DescriptionAttribute`
FroggieFrog opened this issue · 4 comments
Similar to #11 it would be nice to add support for the System.ComponentModel.DescriptionAttribute
.
At least I use it far more often than the System.ComponentModel.DataAnnotations.DisplayAttribute
.
example:
enum MyValues
{
/// <summary>
/// The first value.
/// </summary>
[Description("This is value 1.")]
Value1,
/// <summary>
/// This is an alternative to Value1.
/// </summary>
[Description("An alternative value.")]
Value2
}
That seems reasonable to me! As in #11, I'm not entirely sure what the support should look like. Should it only affect ToStringFast()
? Or should we use it for Parse
and IsDefined
etc too? 🤔 What are your thoughts @FroggieFrog?
I don't really have an opinion on that topic, but what I'm regularly using is a combination of custom extension methods to parse forth and back. (e.g. string ToDescription()
and T ParseByDescription<T>()
)
But I would recommend to be consistent with the implementation in #11. (What ever that would be.)
I have always used DescriptionAttribute
instead of DisplayNameAttribute
.
It would be nice to have both options, if there is [DisplayName]
, it generates the GetDisplayName()
and FromDisplayName(string)
methods, and if there is [Description]
it generates the GetDescription()
and FromDescription(string)
methods.
I just wouldn't use the approach described in #11, I would do something like this:
=> value switch
{
Colour.Red => "The red description"
Colour.Blue => "The blue description"
Colour.Green => nameof(Colour.Green), // when not having the attribute,
_ => value.ToString()
}
I don't think it's a good idea to return the ToString
value of the enum when you specifically asked for the Description or the DisplayName.
What about this:
string GetDescription(Color color) => color switch
{
Color.Red => "The red description",
Color.Blue => "The blue description",
_ => string.Empty,
};
Or:
bool GetDescription(Color color, out string description)
{
switch (color)
{
case Color.Red:
description = "The red description";
return true;
case Color.Blue:
description = "The blue description";
return true;
default:
description = string.Empty;
return false;
}
}