Skip to content

Instantly share code, notes, and snippets.

@hachibeeDI
Created October 5, 2012 09:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hachibeeDI/3839008 to your computer and use it in GitHub Desktop.
Save hachibeeDI/3839008 to your computer and use it in GitHub Desktop.
Convert value as Integer to Enum
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