Possible Duplicate:
Get Enum from Description attribute
Hi All, I have and Enum defined like this.
public enum SomeType {
[Description("One Value")]
One,
[Description("Two Value")]
Two,
[Description("Three Value")]
Three
}
but when I try to parse a string like this
SomeType test = (SomeType )Enum.Parse(typeof(SomeType ), "Three Value");
I get excetion "Requested value 'Three Value' was not found". Isn't this supposed to work ?
Thanks
No, it's not. You can find the Enum by the enum Name ("One", "Two", "Three"), but not by Description (at least not that way). Maybe via Reflection...
You might wanna take a look at this: How to get C# Enum description from value?
Update
Take a look at @KIvanov's comment and look here: Get Enum from Description attribute
As far as I know
SomeType test = (SomeType )Enum.Parse(typeof(SomeType ), "Three");
would do what you want
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