2

I wonder if there is a method to check if the value is the description of an Enum. I know

     Enum.IsDefined(typeof(EnumEntity),value)

could be used to check if the value is in the Enum, but how about the description?

For example,

 public enum LicenseTypes 
{
    [Description("A License")]
    A,
    [Description("B License")]
    B,
    [Description("C License")]
    C
}

Is there a way to check "A License" is a description of enum LicenseTypes?

Steven Zack
  • 4,984
  • 19
  • 57
  • 88
  • Do you want to check A License is a description on any of they enums or just the enum A. – Jace Rhea Mar 22 '13 at 21:53
  • Have you looked at this post? http://stackoverflow.com/questions/695730/how-to-access-the-description-attribute-on-either-a-property-or-a-const-in-c – Michael Dunlap Mar 22 '13 at 21:55

1 Answers1

5

It would go something like this (untested):

bool isPresent = Enum.GetValues(typeof(LicenseTypes))
                     .Select(e => e.GetDescription())
                     .Contains("A License");

where GetDescrtiption() is the helper function defined in answer to this question: Get the Enum<T> value Description

Community
  • 1
  • 1
Pieter Geerkens
  • 11,775
  • 2
  • 32
  • 52