Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add support for System.ComponentModel.DescriptionAttribute #15

Closed
FroggieFrog opened this issue Apr 1, 2022 · 4 comments · Fixed by #46
Closed

add support for System.ComponentModel.DescriptionAttribute #15

FroggieFrog opened this issue Apr 1, 2022 · 4 comments · Fixed by #46

Comments

@FroggieFrog
Copy link

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
}
@andrewlock
Copy link
Owner

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?

@FroggieFrog
Copy link
Author

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.)

@eglauko
Copy link

eglauko commented Apr 12, 2022

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()
}

@adamradocz
Copy link
Contributor

adamradocz commented Jun 11, 2022

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;
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
4 participants