March 21, 2008

Generic Enum to List(Of T) Converter

Joe Niland posted this Generic Enum to List converter, implemented in C#. Not sure if I'll ever use this, but it looks useful. Here is the VB.NET version:

Public Shared Function EnumToList(Of T)() As List(Of T)
 
    Dim enumType As Type = GetType(T)
 
    If enumType.BaseType IsNot GetType(System.Enum) Then
        Throw New ArgumentException("T must be of type System.Enum")
    End If
 
    Return New List(Of T)(CType(System.Enum.GetValues(enumType), IEnumerable(Of T)))
 
End Function

No comments:

Post a Comment