Based on this question, and preferably using this answer along with this answer to get enum attributes, how is it possible to cast the enum to a dictionary where Key is the enum value itself and Value is the description attribute?
Asked
Active
Viewed 1,368 times
3
Community
- 1
- 1
Ivan-Mark Debono
- 13,730
- 22
- 106
- 224
1 Answers
10
Given the GetAttributeOfType<T>() extension method you can simply do:
var dic = Enum.GetValues(typeof(SomeEnum))
.Cast<SomeEnum>()
.ToDictionary(k => k, v => v.GetAttributeOfType<DescriptionAttribute>())
If you directly want the Description in the value:
var dic = Enum.GetValues(typeof(SomeEnum))
.Cast<SomeEnum>()
.ToDictionary(k => k, v => v.GetAttributeOfType<DescriptionAttribute>().Description)
pierroz
- 7,285
- 9
- 46
- 60