Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple enum descriptions

Tags:

I have defined the following enum:

public enum DeviceType {     [Description("Set Top Box")]     Stb = 1,     Panel = 2,     Monitor = 3,     [Description("Wireless Keyboard")]     WirelessKeyboard = 4 } 

I'm utilising the Description attribute to allow me to pull out a more user-readable version of the enum to display in the UI. I get the description using the following code:

var fieldInfo = DeviceType.Stb.GetType().GetField(DeviceType.Stb.ToString());  var attributes = (DescriptionAttribute[])fieldInfo.GetCustomAttributes(typeof(DescriptionAttribute), false);  var description = (attributes.Length > 0 ? attributes[0].Description : DeviceType.Stb.ToString()); 

The above code will give me: description = "Set Top Box". If there is no Description attribute set, it will give me the string value of the enum.

I now want to add a second/custom attribute to each of the enums (called 'Value' for examples sake). eg:

public enum DeviceType {     [Description("Set Top Box")]     [Value("19.95")]     Stb = 1,     [Value("99")]     Panel = 2,     [Value("199.99")]     Monitor = 3,     [Description("Wireless Keyboard")]     [Value("20")]     WirelessKeyboard = 4 } 

I will need to pull out the new Value attribute much the same way I currently do with the Description attribute.

Is it possible to extend the existing Description attribute to somehow include the new Value attribute, or is it best to create the new attribute separately?

like image 395
sfinks_29 Avatar asked Oct 02 '15 06:10

sfinks_29


1 Answers

Create a new attribute seperately called DeviceInformation...

[AttributeUsage(AttributeTargets.All)] public class DeviceInformationAttribute : DescriptionAttribute {     public DeviceInformationAttribute(string description, string value)     {         this.Description = description;         this.Value = value;     }      public string Description { get; set; }     public string Value { get; set; } } 

You can also use the extension method to retrieve the value of any attribute

static void Main(string[] args) {     var info = DeviceType.Stb.GetAttribute<DeviceInformationAttribute>();     Console.WriteLine("Description: {0}\nValue:{1}",info.Description, info.Value);  }  public static class Extensions {     public static TAttribute GetAttribute<TAttribute>(this Enum enumValue)             where TAttribute : Attribute     {         return enumValue.GetType()                         .GetMember(enumValue.ToString())                         .First()                         .GetCustomAttribute<TAttribute>();     } }  public enum DeviceType {     [DeviceInformation("foobar", "100")]     Stb = 1, } 

Edit

In response to the comment

@Aydin Adn I do love the use of the extension method, very nice! Do you have a solution for the case of DeviceType.Panel which does not have a description, but needs the Value attribute? (see comments on Patrick's answer)

 [AttributeUsage(AttributeTargets.All)] public class DeviceInformationAttribute : Attribute {     public DeviceInformationAttribute(string description)     {         this.Description = description;     }      public DeviceInformationAttribute(decimal value)     {         this.Description = string.Empty;         this.Value = value;     }      public DeviceInformationAttribute(string description, decimal value)     {         this.Description = description;         this.Value = value;     }         public string Description { get; set; }     public decimal Value { get; set; } } 
like image 147
Aydin Avatar answered Nov 02 '22 22:11

Aydin