I am pulling a 'long' value from Session, using a generic property, and it is crashing.
so I have:
public static T Get<T>(string key)
{
if(...)
return (T)System.Web.HttpContext.Current.Session[key];
...
}
When debugging, the value is 4, and it crashes.
If you insist on keeping your generic method, you can use Convert.ChangeType():
public static T Get<T>(string key)
{
if (...)
return
(T) Convert.ChangeType(System.Web.HttpContext.Current.Session[key],
typeof(T));
...
}
That will allow you to call:
long n = Get<long>("sessionkey");
But be careful: Convert.ChangeType() doesn't work for all conversions.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With