c# - 随机 InvalidCastException

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

最佳答案

此代码原样,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/

相关文章:

java - 在运行时解析泛型变量时如何避免未经检查的强制转换?

swift - 如何使用 Swift 中的 where 检查/将类转换为泛型类型

C#:在两个不同的定界符之间获取子字符串

c# - 可以用 C# 编写闪存内容吗?

c# - System.Threading.ThreadPool.QueueUserWorkItem C# 的动态回调方法

javascript - 如何根据 session 变量php过滤结果

c# - Directory.GetFiles 上的 LINQ,使用多个排序条件进行过滤和排序

c# - 如何处理 session 中的对象数组

php - Laravel:一次只允许每个用户一个 session

java - 无法转换为子类