从高速缓存转换为通用列表< T>引发异常 [英] cast from cache to generic list<T> throws an exception
本文介绍了从高速缓存转换为通用列表< T>引发异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在Asp.net应用程序中.我将Linq to Xml结果缓存到Cache对象.当我将缓存对象投射回通用列表时.我收到以下异常.
In Asp.net application. i cache a Linq to Xml result to a Cache object. when i cast the cache object back to generic list. i get the following exception.
[A]System.Collections.Generic.List`1[CarRentalAddress] cannot be cast to [B]System.Collections.Generic.List`1
[CarRentalAddress]. Type A originates from 'mscorlib, Version=2.0.0.0, Culture=neutral,
PublicKeyToken=b77a5c561934e089' in the context 'LoadNeither' at location
'C:\WINDOWS\assembly\GAC_32\mscorlib\2.0.0.0__b77a5c561934e089\mscorlib.dll'. Type B originates from 'mscorlib,
Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' in the context 'LoadNeither' at location
'C:\WINDOWS\assembly\GAC_32\mscorlib\2.0.0.0__b77a5c561934e089\mscorlib.dll'.
我的代码如下.
if
(!Page.IsPostBack)
{
if
(Cache["xml"
] == null
)
{
DataSet ds = new
DataSet();
ds.ReadXml(HttpContext.Current.Server.MapPath("~/CarRentalAddress.xml"
));
var
grid = (from
d in
ds.Tables[0].AsEnumerable() orderby
d.Field<string
>("City"
) where
d.Field<string
>("Enabled"
) == "1"
select
new
CarRentalAddress { City = d.Field<string
>("City"
), HotelName = d.Field<string
>("HotelName"
), Address = d.Field<string
>("Address"
), EmailID1 = d.Field<string
>("EmailID1"
), EmailID2 = d.Field<string
>("EmailID2"
) }).ToList();
Cache["xml"
] = grid;
totalrows = grid.Count().ToString();
grdAddress.DataSource = grid;
grdAddress.DataBind();
}
else
{
//Exception is thrown by this line
var
grid = (List<CarRentalAddress>)Cache["xml"
];
totalrows = grid.Count().ToString();
grdAddress.DataSource = grid;
grdAddress.DataBind();
}
}
推荐答案
确保所有库都是使用正确的配置(任何CPU或x86)构建的.然后,清理您的整体解决方案并重建它,然后重试.
Make sure that all libraries are built using the right configuration (Any CPU or x86). Then, clean your total solution and rebuild it and try again.
这篇关于从高速缓存转换为通用列表< T>引发异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文