非静态字段,方法或属性所需的对象引用

时间:2009-08-19 21:55:14

标签: c#

我想使用Caching.Cache(...)方法,如下所示:

Cache.Insert("Interview Questions", datatable, sqlcachedep)

System.Web.Caching.Cache.Insert("Reading List", datatable, sqlcachedep);

变量没有问题,但在任何一种情况下都会收到此错误消息:

  

错误1 - 非静态字段,方法或属性'System.Web.Caching.Cache.Insert(string,object,System.Web.Caching.CacheDependency)'

需要对象引用

我该如何解决这个问题?

由于

3 个答案:

答案 0 :(得分:16)

这是正确的说法。你应该尝试类似的东西:

HttpContext.Current.Cache.Insert(...);

Cache.Insert不是静态方法(静态方法在文档中的方法图标附近用“S”表示。)您需要一个实例来调用Insert方法。 HttpContext.Current.Cache返回与当前应用程序关联的Cache对象。

答案 1 :(得分:1)

你需要做

Page.Cache.Insert()

(我假设你在谈论ASP.Net)。你正在调用Cache作为类,而不是它的实例。

答案 2 :(得分:1)

试试这个(从内存中):

HttpApplication.Context.Cache.Insert("Reading List", datatable, sqlcachedep);