Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# Enum with description not working [duplicate]

Tags:

c#

enums

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

like image 219
dcmovva Avatar asked Sep 05 '25 18:09

dcmovva


2 Answers

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

like image 63
Adriano Carneiro Avatar answered Sep 10 '25 13:09

Adriano Carneiro


As far as I know

SomeType  test =  (SomeType )Enum.Parse(typeof(SomeType ), "Three");

would do what you want

like image 45
m.edmondson Avatar answered Sep 10 '25 14:09

m.edmondson