0

Let's say I have an enum, was wondering how can I get value Foo based provided description "Description for Foo"?

public enum MyEnum
{
    [Description("Description for Foo")]
    Foo,
    [Description("Description for Bar")]
    Bar
}
Trevor
  • 7,777
  • 6
  • 31
  • 50
leta8895
  • 1
  • 1
  • I think you can use it this way https://stackoverflow.com/a/1799401/11332666 – leejulee Mar 05 '21 at 18:50
  • This answers the question more directly https://stackoverflow.com/questions/4367723/get-enum-from-description-attribute – Jasen Mar 05 '21 at 18:52
  • Rather this: https://stackoverflow.com/questions/3422407/finding-an-enum-value-by-its-description-attribute . But note that you should cache these values in a class that has a `static Dictionary DescriptionCache { get; }`. Enums never change at runtime, so this is a perfect candidate for caching. You don't want to use reflection any time you access your enum. – Tim Schmelter Mar 05 '21 at 18:56

0 Answers0