3

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

Community
  • 1
  • 1
dcmovva
  • 145
  • 3
  • 10

2 Answers2

1

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

Community
  • 1
  • 1
Adriano Carneiro
  • 55,739
  • 12
  • 86
  • 122
1

As far as I know

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

would do what you want

m.edmondson
  • 29,632
  • 26
  • 117
  • 199