Skip to content

Instantly share code, notes, and snippets.

@digiguru
Created December 17, 2012 10:30
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 digiguru/4317308 to your computer and use it in GitHub Desktop.
Save digiguru/4317308 to your computer and use it in GitHub Desktop.
Public Module SessionExtensions
<Runtime.CompilerServices.Extension()>
Public Function TryGetValue(session As System.Web.SessionState.HttpSessionState,
sessionItemName As String,
ByRef result As Object) As Boolean
If session Is Nothing Then
Throw New ArgumentNullException("Session", "Session is nothing.")
End If
If String.IsNullOrEmpty(sessionItemName) Then
Throw New ArgumentException("SessionItemName is nothing or empty.", "SessionItemName")
End If
result = session(sessionItemName)
Return result IsNot Nothing
End Function
<Extension()>
Public Function TryConvertValueOf(Of T As {Class})(ByVal sess As HttpSessionState,
ByVal param As String,
ByRef output As T) As Boolean
Dim obj As Object = sess(param)
If TypeOf obj Is T Then
output = DirectCast(obj, T)
Return output IsNot Nothing
End If
Return False
End Function
End Module
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment