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,474 times
3

Community
- 1
- 1

Ivan-Mark Debono
- 15,500
- 29
- 132
- 263
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,653
- 9
- 48
- 60