Return to Snippet

Revision: 61997
at January 26, 2013 21:14 by dimitrisdapontes


Initial Code
Imports System.Reflection
Imports System.ComponentModel

''' <summary>
''' Returns the enum description (if any), otherwise returns the name of the enum value 
''' </summary>    
Friend Shared Function GetEnumDescription(Of TEnum)(enumObj As TEnum) As String
    Dim fi As FieldInfo = enumObj.GetType().GetField(enumObj.ToString())

    Dim attributes As DescriptionAttribute() = fi.GetCustomAttributes(GetType(DescriptionAttribute), False)

    If attributes IsNot Nothing AndAlso attributes.Length > 0 Then
        Return attributes(0).Description
    Else
        Return enumObj.ToString()
    End If
End Function

Initial URL


Initial Description
<p>Returns the description set in an enum value, or if no description is found it returns the name of the enum value. Use this without worrying about passing the enum type, you should only pass the enum value</p>

<p>Example</p>

<pre><code>Friend Enum Duration
    &lt;Description("Every 12 Months")&gt; Year = 1
    &lt;Description("Every 6 Months")&gt; Semester = 2
End Enum

GetEnumDescription(Duration.Year) returns "Every 12 Months"
</code></pre>

Initial Title
Enum Description

Initial Tags


Initial Language
VB.NET