所需的非静态字段,方法或属性的对象引用 [英] Object reference required for non-static field, method, or property
问题描述
我想使用的Caching.Cache(...)方法,像这样:
I want to use the Caching.Cache(...) method, like so:
Cache.Insert("Interview Questions", datatable, sqlcachedep)
或
System.Web.Caching.Cache.Insert("Reading List", datatable, sqlcachedep);
有与变量没有问题,但我得到在任何情况下,这个错误消息:
There is no problem with the variables, but I get this error message in either case:
错误1 - 一个对象引用所需的非静态字段,方法或属性System.Web.Caching.Cache.Insert(串,对象,System.Web.Caching.CacheDependency)
Error 1 - An object reference is required for the non-static field, method, or property 'System.Web.Caching.Cache.Insert(string, object, System.Web.Caching.CacheDependency)'
我该如何解决这个问题?
How can I fix this?
感谢
推荐答案
它说正确的事情。你应该尝试是这样的:
It's saying the correct thing. You should try something like:
HttpContext.Current.Cache.Insert(...);
的 Cache.Insert
是不是一个静态方法(静态方法由一个表示S附近文档中的法图标。)您需要一个实例来调用上插入
方法。 HttpContext.Current.Cache
返回与当前应用程序相关的缓存
对象。
Cache.Insert
is a not a static method (static methods are indicated by an "S" near the method icon in the documentation.) You need an instance to call the Insert
method on. HttpContext.Current.Cache
returns the Cache
object associated with the current application.
这篇关于所需的非静态字段,方法或属性的对象引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!