Я получаю это исключение в словарях и списках, которые имеют собственный класс.
Пример:
List<DisplayAllQuestionsTable> dsa = (List<DisplayAllQuestionsTable>)Session["Display"];
Когда я использую сессию, приведение в действие работает 10-20 раз, а затем оно начинает выдавать исключение Если я живу на компьютере в течение 20-30 минут ... я могу запустить мое веб-приложение как обычно, и после 20 раз запуска кода, оно выдает то же исключение. Почему это происходит?
Теперь я протестировал еще один более простой код с Сессоном:
public partial class Default2 : System.Web.UI.Page
{
List<meem> moom = new List<meem>();
protected void Page_Load(object sender, EventArgs e)
{
for (int i = 0; i < 20; i++)
{
meem m = new meem();
m.i = i;
moom.Add(m);
}
Session["meem"] = moom;
Button ew = new Button();
ew.Text = "Press me";
ew.Click += Click;
PlaceHolder1.Controls.Add(ew);
}
void Click(object sender, EventArgs e)
{
List<meem> moom = (List<meem>)Session["meem"];
foreach (var item in moom)
{
Label l = new Label();
l.Text = item.i.ToString();
this.Controls.Add(l);
}
}
}
class meem
{
public int i;
}
И это работает 100%
Исключение, которое я получаю:
Server Error in '/WebSite10' Application.
[A]System.Collections.Generic.List`1[DisplayAllQuestionsTable] cannot be cast to
[B]System.Collections.Generic.List`1[DisplayAllQuestionsTable].
Type A originates from 'mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' in the context 'LoadNeither'
at location 'D:\WINDOWS\Microsoft.Net\assembly\GAC_32\mscorlib\v4.0_4.0.0.0__b77a5c561934e089\mscorlib.dll'.
Type B originates from 'mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' in the context 'LoadNeither'
at location 'D:\WINDOWS\Microsoft.Net\assembly\GAC_32\mscorlib\v4.0_4.0.0.0__b77a5c561934e089\mscorlib.dll'.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.InvalidCastException: [A]System.Collections.Generic.List`1[DisplayAllQuestionsTable] cannot be cast to [B]System.Collections.Generic.List`1[DisplayAllQuestionsTable]. Type A originates from 'mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' in the context 'LoadNeither' at location 'D:\WINDOWS\Microsoft.Net\assembly\GAC_32\mscorlib\v4.0_4.0.0.0__b77a5c561934e089\mscorlib.dll'. Type B originates from 'mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' in the context 'LoadNeither' at location 'D:\WINDOWS\Microsoft.Net\assembly\GAC_32\mscorlib\v4.0_4.0.0.0__b77a5c561934e089\mscorlib.dll'.