gpt4 book ai didi

c# - 随机 InvalidCastException

转载 作者:行者123 更新时间:2023-11-30 18:04:15 28 4
gpt4 key购买 nike

我在具有自定义类的字典和列表中遇到了该异常。示例:

 List<DisplayAllQuestionsTable> dsa = (List<DisplayAllQuestionsTable>)Session["Display"];

当我使用 Session 时,转换工作了 10-20 次..然后它开始抛出异常。如果我让电脑开机大约 20-30 分钟..我可以像往常一样启动我的 Web 应用程序,并且在启动代码 20 次后,它会抛出相同的异常。为什么会这样?

现在我用 Sesson 测试了另一个更简单的代码:

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'.

最佳答案

此代码原样,List<DisplayAllQuestionsTable> dsa = (List<DisplayAllQuestionsTable>)Session["Display"];

仅当您尝试使用它时才不会导致空引用异常。

类似这段代码List<test> l = (List<test>)Session["test"];如果 Session["test"] 将不会导致 null 或无效的转换异常一片空白。仅当 Session["test"] 时才会发生无效的转换异常不为空。在我看来,存储在 Display 中的对象已经以某种方式变形了。

关于c# - 随机 InvalidCastException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6370252/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com