这很奇怪,因为无法转换 DataCacheItem
列出集合。
我正在从数据库中提取值并将其存储到缓存中,但如果我要求缓存使用 TypeCast 返回数据,那么它会拒绝这样做。
Cannot cast 'isValueInCache' (which has an actual type of
'Microsoft.ApplicationServer.Caching.DataCacheItem') to
'System.Collections.Generic.List<MMD.Data.HumanGender>'
//Setting Value in Cache Object
var isValueInCache = BaseCache.Instance().RetrieveCacheValue("allGenders");
//TypeCasting of Data
var isSeariled = isValueInCache == null ?
Newtonsoft.Json.JsonConvert.DeserializeObject(proxy.GetAllGenders(),
typeof(List<HumanGender>)) as List<HumanGender>
: isValueInCache as List<HumanGender>;
我还没有找到为什么它无法转换 Object
至List<T>
。我似乎只能在这里工作,将对象转换到 JSON
格式,按键拉动并创建 List Object
。
Update 1:
这种情况不仅涉及列表,还涉及从缓存检索的任何实体对象。
Update 2:
也不使用 DTO。
仍在寻找解决办法。
最佳答案
您不应该使用数据缓存项的 value 属性吗?
目前不在 Visual Studio 后面,所以我在没有编译器的情况下编写此内容。
var isValueInCache = BaseCache.Instance().RetrieveCacheValue("allGenders") as DataCacheItem;
//TypeCasting of Data
var isSeariled = isValueInCache == null ?
Newtonsoft.Json.JsonConvert.DeserializeObject(proxy.GetAllGenders(),
typeof(List<HumanGender>)) as List<HumanGender>
: isValueInCache.Value as List<HumanGender>;
编辑:
我认为 BaseCache 的 RetrieveCacheValue 方法返回了错误的类型。
关于c#-4.0 - Azure 缓存对象的类型转换始终为 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17832153/