随机InvalidCastException

时间:2011-06-16 10:13:09

标签: c# session casting

我在具有自定义类的词典和列表中获得该异常。 例如:

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

2 个答案:

答案 0 :(得分:1)

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

只有在您尝试使用它时,

才会导致空引用异常。

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

答案 1 :(得分:0)

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

在您的点击处理程序中,首先检查会话对象是否存在,或者在迭代之前为空。

<强>更新

稍后看到您的例外详情。这篇文章会有帮助吗? InvalidCastException when serializing and deserializing同时检查标记答案中的“加载器上下文”链接。

希望这可以帮助您进一步跟踪您的例外情况。