I just hate loosing object types such as you do in when retreving objects from the ASP.NET session, like done here
Wouldn’t it be nice if we handled this more gracfully? And actually got the object back we expected? This could easily be accomplished using extension methods.
I’ve created several methods to get the value from session. The Get method just gets the object and casts it to the specified object, the downside here is the possibility for an InvalidCastException. The TryGet method, uses the standard .NET way, and uses and out parameter. The last method, SafeGet, is my prefered way (I just hate out parameters), and returns an object which contains the success and the value. Each method can be used like this
This method can of course easily be adapted to similar situations like TempData.