Random InvalidCastException

本文关键字:InvalidCastException Random | 更新日期: 2023-09-27 17:59:13

我在有自定义类的Dictionaries和Lists中得到了这个异常。示例:

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

当我使用会话时,演员会工作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'.

Random InvalidCastException

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

只有当您尝试使用空引用时,它才会导致空引用异常。

类似地,如果Session["test"]为null,则此代码List<test> l = (List<test>)Session["test"];不会导致null或无效强制转换异常。只有当Session["test"]不为null时,才会发生无效的强制转换异常。在我看来,存储在Display中的对象在某种程度上发生了变形。

听起来像是你的会话时间。(默认为20分钟)

在您的点击处理程序中,首先检查会话对象是否存在或为空,然后再对其进行迭代

更新

稍后看到您的异常详细信息。这篇文章会有帮助吗?序列化和反序列化时出现InvalidCastException还请检查标记答案中的"loader-contexts"链接。

希望这将帮助您进一步跟踪您的异常。

相关文章:
  • 没有找到相关文章