gpt4 book ai didi

c# - 缓存包含数据,但无法检索数据

转载 作者:行者123 更新时间:2023-11-30 22:43:43 24 4
gpt4 key购买 nike

这是我的代码:

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);
}

当我尝试从缓存中提取数据时,我的“sales”对象为空。想知道为什么该代码会被命中,我在 VS 中运行调试器以查看 Cache 对象中的内容。

缓存包含我需要的数据,但是当它从缓存中获取数据时,“sales”仍然为空。

我这里有什么地方做错了吗?

编辑:

我在转换时遇到此错误:

[A]System.Collections.Generic.List1[controls_mySales+Sale] cannot be cast to [B]System.Collections.Generic.List1[controls_mySales+Sale]. 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'

最佳答案

as keyword 上的 MSDN 页面指出:

The as operator is like a cast except that it yields null on conversion failure instead of raising an exception.

看起来这就是这里发生的事情——强制转换为类型 List<Sale>正在失败,并返回 null .您确定这是缓存中对象的类型吗?

编辑:

作为对您编辑的回应,似乎与 binding contexts 相关的某种程序集相关序列化/反序列化问题老实说,这有点让我头疼。我四处查看,发现以下两个关于 SO 的问题可能会为您指明正确的方向:

Question 1
Question 2

希望这些链接对您有所帮助。

关于c# - 缓存包含数据,但无法检索数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3815975/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com