2013-03-22 115 views
0

我想知道是否有方法檢查值是否是Enum的描述。 我知道C#枚舉如何檢查枚舉中是否存在描述

 Enum.IsDefined(typeof(EnumEntity),value) 

可以用來檢查值在枚舉,但如何描述?

例如,

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

有沒有一種方法來檢查「許可證」是枚舉LicenseTypes的描述?

+0

你要檢查許可證是說明他們的任何枚舉或只是枚舉A. – 2013-03-22 21:53:12

+0

你看過這個職位? http://stackoverflow.com/questions/695730/how-to-access-the-description-attribute-on-either-a-property-or-a-const-in-c – 2013-03-22 21:55:08

回答

1

它會去像這樣(未經):

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

其中GetDescrtiption()是在回答定義這個問題的輔助函數: Get the Enum<T> value Description