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

  • 相关阅读:
    时空上下文视觉跟踪(STC)算法的解读与代码复现(转)
    神经网络:卷积神经网络(转)
    图像卷积与滤波的一些知识点(转)
    Deep Learning论文笔记之(四)CNN卷积神经网络推导和实现(转)
    深度学习(卷积神经网络)一些问题总结(转)
    卷积神经网络的初步理解LeNet-5(转)
    中期总结
    转载:通俗理解遗传算法
    转载:协方差与协方差矩阵
    装载:关于拉格朗日乘子法与KKT条件
  • 原文地址:https://www.cnblogs.com/CoreXin/p/4857175.html
Copyright © 2011-2022 走看看