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?
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With