I have a very simple method that looks like this:
public void DoSmth(int y)
{
XXXXX x = (XXXXX)Session["key"];
x.DoSmthElse(y);
}
But I get an exception:
System.InvalidCastException: Unable to cast object of type Z.YYYYY to type Z.XXXXX
at XYZ.DoSmth(Int32 y)
Exception itself makes sense, but why possibly does it occur? What can I do to prevent it? Why does Session["key"] have type there?
Exception itself makes sense, but why possibly does it occur?
What can I do to prevent it?
You can check if Session["key"] contains the right type like this
if (Session["key"] is XXXXX x) { x.DoSmthElse(y); }
Why does Session["key"] have type there?
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.