我有一个看起来像这样的类:
using System.Collections.Generic;
using System.Web.Caching;
public static class MyCache
{
private static string cacheKey = "mykey";
public static Dictionary<string, bool> GetCacheValue(bool bypassCache)
{
var settings = Cache[cacheKey] as Dictionary<string, bool>; // error on this line
// ...etc...
return settings
}
}我遇到的问题是这个不能编译。编译器说Cache不能像我这样使用。下面是要传达的信息:
'System.Web.Caching.Cache' is a 'type' but is used like a 'variable'这让我很困惑。我用谷歌搜索了ASP.NET缓存API,发现了许多以这种方式使用Cache的示例。下面是其中的一个例子:
// http://www.4guysfromrolla.com/articles/100902-1.aspx
value = Cache("key")
- or -
value = Cache.Get("key")当我尝试使用Cache.Get()时,我得到另一个错误,告诉我它不是一个静态方法。
显然,我需要初始化Cache的一个实例。这是正确的API使用方法吗?一个后续问题是,缓存的信息是否会跨实例持续存在?
谢谢你的帮助。
发布于 2013-09-27 23:14:10
System.Web.Caching.Cache是一个类-您可以看到人们使用一个名为Cache的属性,该属性是System.Web.Caching.Cache的一个实例。如果在为您提供Cache属性的类外部使用它,请使用System.Web.HttpRuntime.Cache访问它
var settings = System.Web.HttpRuntime.Cache[cacheKey] as Dictionary<string, bool>;https://stackoverflow.com/questions/19054137
复制相似问题