Skip to content

Instantly share code, notes, and snippets.

@wonderful-panda
Created July 5, 2012 15:13
Show Gist options
  • Save wonderful-panda/3054264 to your computer and use it in GitHub Desktop.
Save wonderful-panda/3054264 to your computer and use it in GitHub Desktop.
あれば便利な気がするけどわざわざ作るほどでもない気もする拡張メソッドたち
Public Module Extensions
<Extension()> _
Public Function This(Of T)(ByVal target As T) As T
Return target
End Function
<Extension()> _
Public Function IsNull(Of T As Class)(ByVal target As T) As Boolean
Return target Is Nothing
End Function
<Extension()> _
Public Function IsNotNull(Of T As Class)(ByVal target As T) As Boolean
Return target IsNot Nothing
End Function
<Extension()> _
Public Function Or_(Of T As Class)(ByVal target As T, ByVal Func(Of T) factory) As T
If target Is Nothing Then
Return factory()
Else
Return target
End If
End Function
<Extension()> _
Public Function IsOneOf(Of T)(ByVal target As T, ParamArray candidates() As T) As Boolean
Return candidates.Contains(target)
End Function
End Module
'' 使いみち
'' This/IsNull/IsNotNull は Withと一緒に
With GetFooInstance()
If .IsNull Then
Return False
End If
.DoSomething()
list.Add(.This)
End With
'' IsOneOf は If文で
If a.IsOneOf(1, 2, 3) Then '' If a = 1 OrElse a = 2 OrElse a = 3 Then の代わりに
...
End If
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment