【发布时间】:2011-04-18 11:24:40
【问题描述】:
这是我的代码:
List<Sale> sales = new List<Sale>();
if (Cache["Sales"] != null)
{
sales = (List<Sale>)Cache["Sales"];
}
else
{
...
Cache.Add("Sales", sales, null, DateTime.Now.AddMinutes(20),
Cache.NoSlidingExpiration, CacheItemPriority.Normal, null);
}
当我尝试从缓存中提取数据时,我的“销售”对象为空。想知道为什么该代码会被命中,我在 VS 中运行了调试器以查看 Cache 对象中的内容。
缓存包含我需要的数据,但是当它从缓存中获取数据时,“销售”仍然为空。
我在这里做错了吗?
编辑:
我在投射时遇到此错误:
[A]System.Collections.Generic.List
1[controls_mySales+Sale] cannot be cast to [B]System.Collections.Generic.List1[controls_mySales+Sale]。类型 A 源自位置 'C:\WINDOWS\assembly\GAC_32\mscorlib\2.0.0.0__b77a5c561934e089\mscorlib.dll' 的上下文 'LoadNeither' 中的 'mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' .类型 B 源自位置 'C:\WINDOWS\assembly\GAC_32\mscorlib\2.0.0.0__b77a5c561934e089\mscorlib.dll' 的上下文 'LoadNeither' 中的 'mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'
【问题讨论】:
-
这是您代码库中唯一为该缓存键分配值的地方吗?
-
您是否尝试过对
List<Sale>进行强制转换,而不是使用as运算符?可能存在选角问题。 -
如果存储
Sale而不是List<Sale>会发生什么?你得到什么转换错误?