Created
October 5, 2012 09:52
-
-
Save hachibeeDI/3839008 to your computer and use it in GitHub Desktop.
Convert value as Integer to Enum
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Imports System.Runtime.CompilerServices | |
Module EnumUtils | |
<Extension()> _ | |
Public Function convertEnum(Of TEnum)(this As Integer) As TEnum | |
Dim t As TEnum | |
If Not t.GetType().IsEnum Then | |
Throw New ArgumentException("Enum型以外はダメですよん") | |
End If | |
Dim values = [Enum].GetValues(GetType(TEnum)).Cast(Of Integer)().ToList | |
If values.Contains(this) Then | |
Return [Enum].Parse(GetType(TEnum), this) | |
Else | |
Thorw New ArgumentOutOfRangeException("元の列挙体定義に存在しない値ですよん") | |
End If | |
End Function | |
End Module |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment