0

I have a Enum and want to add a description foreach value. I read something about attributes, which could solve my problem. I try to implement the solution from msdn but without succes. It works if iam using a class but not with enum values.

Is it possible that someone post how that works?

regards

Heres a link to the msdn solution. Thats what i try to do. Only difference is that i want to do with enum value.

http://msdn.microsoft.com/de-de/library/aa288454(v=vs.71).aspx

Example:

public enum Color
{
  [Description("This is red")]
  Red,
  [Description("This is blue")]
  Blue
}

how can i get acces to the description and how i have to implement the description class/method?

Sebi
  • 3,879
  • 2
  • 35
  • 62

2 Answers2

2

Something like this

using System.ComponentModel;

public enum MyColors{
   [Description("Editor Color")]
   White,
   [Description("Errors Color")]
   Red,
   [Description("Comments Color")]
   Green
}
Patrick D'Souza
  • 3,491
  • 2
  • 22
  • 39
  • Thats exactly what i want to do. But how can i get acces on description. Thats my problem. – Sebi Apr 15 '13 at 06:01
0

Try this for enum

public enum MyColors{
[Description("Editor Color")]
White,
[Description("Errors Color")]
Red,
[Description("Comments Color")]
Green
}


var type = typeof(MyColors);
var memInfo = type.GetMember(MyColors.White.ToString());
var attributes = memInfo[0].GetCustomAttributes(typeof(DescriptionAttribute),
false);
var description = ((DescriptionAttribute)attributes[0]).Description;
Rajeev Kumar
  • 4,901
  • 8
  • 48
  • 83
  • 2
    I am pretty sure you just copied it from http://stackoverflow.com/questions/1799370/getting-attributes-of-enums-value – Nahum Apr 15 '13 at 06:13
  • @NahumLitvin well Dear, I have already used this code once in my application using this post. So i just copy and paste it from my code where i hv used it.. – Rajeev Kumar Apr 15 '13 at 06:15