I have a class with string constants:
public static class Days
{
[Description("Wow!")]
public const string Mon = "Hi!";
}
I've found that it is possible for enum
to have an extension method to read Description
attribute:
using System.ComponentModel;
public enum Days
{
[Description("Wow!")]
Mon
}
An extension method for enum
:
public static string ToName(this Enum value)
{
var attribute = value.GetAttribute<DescriptionAttribute>();
return attribute == null ? value.ToString() : attribute.Description;
}
And call it like that:
Days.Mon.ToName()
Is it possible to write an extension method for string
to get Wow!
from Description
attribute of Mon
string variable and call extension method for string like that?
string description = Days.Mon.ToName(); // Output: "Wow!"
Aucun commentaire:
Enregistrer un commentaire