zoukankan      html  css  js  c++  java
  • [A]List`1[MyObject] cannot be cast to [B]List`1[MyObject]

    Description
    
     I have created a small class in a single ASP.NET 4.5 web forms page that is instantiated and then stored in a List, then saved/loaded from ViewState:
    
    <Serializable>
    Private Class _PageLevelToolTip
        Public ClientId As String
        Public TipText As String
    End Class
    
    Private Property _pageLevelToolTipCollection As List(Of _PageLevelToolTip)
        Get
            Return ViewState("_toolTipCollection")
        End Get
        Set(value As List(Of _PageLevelToolTip))
            ViewState("_toolTipCollection") = value
        End Set
    End Property
    
    This class only appears on one page in the whole application.  
    Users are reporting an intermittent error on subsequent page loads:
    
        [A]System.Collections.Generic.List1[selfassessment+_PageLevelToolTip] cannot be cast to [B]System.Collections.Generic.List1[selfassessment+_PageLevelToolTip].
    
        Type A originates from 'mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' in the context 'LoadNeither' at location 'C:WindowsMicrosoft.NetassemblyGAC_64mscorlibv4.0_4.0.0.0__b77a5c561934e089mscorlib.dll'. Type B originates from 'mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' in the context 'LoadNeither' at location 'C:WindowsMicrosoft.NetassemblyGAC_64mscorlibv4.0_4.0.0.0__b77a5c561934e089mscorlib.dll'.
    
    After searching for days I found a tip that seems to solve the issue, by casting to the type explicitly:
    
    Private Property _pageLevelToolTipCollection As List(Of _PageLevelToolTip)
        Get
            Return CType(ViewState("_toolTipCollection"), List(Of _PageLevelToolTip)) ' Revision here '
        End Get
        ' Setter removed for brevity '
    End Property
    
    Is this a bug?  
    We cannot test this accurately because of the wildly intermittent nature. This is a page-level private object and list that only exist in a single page.
    
    Although the error does not trigger in the above manner, I can recreate it by doing the following process:
    
        1. Load page (ensuring class is saved within viewstate)
        2. Edit page in VS and save
        3. Load page (to load class from Viewstate)
    
    This would imply that the object changes in some way part-way through postbacks, although it's structure does not, or the compiled page changes and won't recognise the original. 
    
    I hope this helps in the diagnosis.  Another person is reporting the same fault from a different approach, but the answer given does not help here: https://connect.microsoft.com/VisualStudio/feedback/details/779108/xaml-designer-in-vs2012-throws-invalidcastexception

     來源:http://cncc.bingj.com/cache.aspx?q=%5bA%5dSystem.Collections.Generic.List+cannot+be+cast+to+%5bB%5dSystem.Collections.Generic.List&d=4976373898546130&mkt=zh-CN&setlang=zh-CN&w=p1PWhpihlQ3W_kwkM-AEL10hUnZb69sm

  • 相关阅读:
    Activity工作流(2)-入门安装运行第一个例子
    三分钟明白 Activiti工作流 -- java运用
    java 实现一套流程管理、流转的思路(伪工作流) 【仅供参考】
    jquery 的 each 方法中 return 的坑
    js判断数组里是否有重复元素的方法
    JFreeChart工具类
    JQuery中$.each 和$(selector).each()的区别详解
    echarts彩虹柱状图 每个bar显示不同颜色, 标题在不同位置 ,工具中有可以直接保存为图片下载,平均线的添加
    poi导出word表格详解 超详细了
    java使用POI操作XWPFDocument中的XWPFRun(文本)对象的属性详解
  • 原文地址:https://www.cnblogs.com/CoreXin/p/4857175.html
Copyright © 2011-2022 走看看