Cache是​​一个tpe,不能用作表达式错误 [英] Cache is a tpe and cannot be used as an expression error

查看:79
本文介绍了Cache是​​一个tpe,不能用作表达式错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看到在C#中使用ASP.NET 2.0 Cache对象的几个参考,其中

文档只显示这样的片段:

if(Cache [&some; sometoken" ;] == null)

...做点什么


但是当我在从ASP.NET调用的C#类中使用此代码时br />
页面,编译器给出了这个错误:

''Cache''是一个tpe,不能用作表达式


此外,当我输入缓存并按下。时,对于intellisense,

只有弹出的东西是Equals,NoAbsoluteExpiration,

NoSliderExpiration,ReferenceEquals。没有添加,插入等方法,所以

出现了。


这让我疯了一会儿。然后我决定直接在我的ASP.NET代码页面中尝试我的缓存代码

,它运行得很好。


最终在捣乱了一段时间后我发现如果我在我的

C#类文件中这样做,那么事情就能正常工作:


public Cache MyCache;

MyCache = System.Web.HttpContext.Current.Cache;


现在我可以在我的C#类中使用if(MyCache [" sometoken''] == null) />
工作正常。


几个问题:


1)这是真的有必要还是我引用或在

中正确执行某些操作?


2)是否有另一种从C#类访问缓存的方法,名为

by a ASP.NET代码背后,还是我偶然发现了我在这里使用的正确方法?


希望我能得到一些澄清什么是/正在发生什么以及为什么

这是必要的,希望有人会发现这篇文章有一天它会给他们节省很多时间。


谢谢,


史蒂夫

解决方案

史蒂夫,


这就是你必须要做的。缓存对象由.aspx页面本地使用
,因此该对象已经被实例化。在一个类

文件中,Cache对象尚未被使用,因此您必须明确地为它创建一个持有者,然后获取页面上使用的缓存对象。


短篇小说,你做得非常好。


-

此致,


S. Justin Gengo,MCP

Web开发人员/程序员

www.aboutfortunate.com


混乱无序。

Nietzsche

Steve Franks < PL **** @ postreplyhere.com>在消息中写道

新闻:rY ******************** @ comcast.com ...

我在C#中看到几个使用ASP.NET 2.0 Cache对象的参考文件,其中文档只显示如下所示的片段:
if(Cache [" sometoken"] == null)
..做某事

然而,当我在从ASP.NET页面调用的C#类中使用此代码时,编译器会出现此错误:
''Cache''是一个tpe,不能用作表达式

此外,当我输入Cache并按下。时,对于intellisense,只有弹出的东西是Equals,NoAbsoluteExpiration,
NoSliderExpiration,ReferenceEquals。没有像Add,Insert等方法出现。

这让我疯了一会儿。然后我决定直接在我的ASP.NET代码页面中尝试我的缓存代码,它工作得很好。

最后在我捣乱了一段时间后,我发现如果我这样做了
我的C#类文件然后工作正常:

公共缓存MyCache;
MyCache = System.Web.HttpContext.Current.Cache;

和现在我可以在我的C#类中使用if(MyCache [" sometoken''] == null)并且
它工作得很好。

有几个问题:

1)这是非常必要的还是我正确地引用或做了什么?

2)是否有另一种从C#类访问Cache的方法
通过后面的ASP.NET代码,或者我是否偶然发现了我在这里使用的正确方法?

希望我能得到一些关于什么/正在进行的澄清为什么
这是必要的,希望有一天会有人找到这篇文章,这将为他们节省很多时间。
谢谢,

史蒂夫



确保System.Web.dll是您的班级所在项目的引用并且要获得asp.net使用的当前Cache对象,请调用

System.Web.HttpContect.Current.Cache


感谢。在相关的说明中,我现在很难在从我的ASP.NET代码调用的同一个C#类中找到Page

对象。


例如,我可以在后面的ASP.NET代码中执行以下操作:

Repeater1.ItemTemplate = Page.LoadTemplate(" mytemplate.ascx");

但是我希望能够从我的C#类中做同样的事情,但是我不知道如何从我的
$中挂钩当前的Page实例。 b $ b C#class。


例如,当涉及到Cache对象时,我发现我必须使用System.Web.HttpContext来使用
.Current.Cache挂钩到Cach对象

实例。我假设我需要做一些类似于获取当前

页面对象的东西?


我一直在四处寻找,但没有系统。 Web.HttpContext.Current.Page

对象我无法弄清楚如何进入当前页面以便我可以从我的C#类中执行Page.LoadTemplate 。


你能告诉我我错过了什么吗?谢谢!


史蒂夫

" S。 Justin Gengo < sjgengo @ [no_spam_please] aboutfortunate.com>写在

消息新闻:英国************** @ TK2MSFTNGP09.phx.gbl ...

史蒂夫,
这就是你必须要做的。缓存对象由.aspx页面本地使用,因此该对象已经被实例化。在一个
类文件中,Cache对象尚未被使用,因此您必须为其明确创建一个holder,然后在页面上获取正在使用的缓存对象。

简短的故事,你做得恰到好处。

-
真诚的,

S. Justin Gengo,MCP
网站开发人员/程序员

www.aboutfortunate.com


I see several reference to using the ASP.NET 2.0 Cache object in C# where
the documentation just shows snippets like this:
if (Cache["sometoken"] == null)
... do something

However when I use this code in a C# class that is called from an ASP.NET
page, the compiler gives this error:
''Cache'' is a tpe and cannot be used as an expression

Furthermore when I type in Cache and press the "." for intellisense, the
only things that pop up are Equals, NoAbsoluteExpiration,
NoSliderExpiration, ReferenceEquals. No methods like Add, Insert and so
forth show up.

This drove me crazy for a while. Then I decided to try my cache code
directly in my ASP.NET code behind page and it worked just fine.

Eventually after mucking around for a while I found that if I did this in my
C# class file then things worked correctly:

public Cache MyCache;
MyCache = System.Web.HttpContext.Current.Cache;

and now I can use if (MyCache["sometoken''] == null) from my C# class and it
works just fine.

A couple of questions:

1) Is this really necessary or am I referencing or doing something in
correctly?

2) Is there another approach to accessing the Cache from a C# class called
by a ASP.NET code behind, or did I stumble upon the right approach by what I
am using here?

Hopefully I''ll get some clarification as to what is/was going on and why
this was necessary, and hopefully someone will find this post one day and it
will save them a lot of time.

Thanks,

Steve

解决方案

Steve,

That''s what you had to do. The Cache object is used by the .aspx page
natively and therefore the object has already been instantiated. In a class
file the Cache object is not already being used so you had to explicitly
create a holder for it and then get the cache object being used on the page.

Short story, you''re doing it exactly right.

--
Sincerely,

S. Justin Gengo, MCP
Web Developer / Programmer

www.aboutfortunate.com

"Out of chaos comes order."
Nietzsche
"Steve Franks" <pl****@postreplyhere.com> wrote in message
news:rY********************@comcast.com...

I see several reference to using the ASP.NET 2.0 Cache object in C# where
the documentation just shows snippets like this:
if (Cache["sometoken"] == null)
... do something

However when I use this code in a C# class that is called from an ASP.NET
page, the compiler gives this error:
''Cache'' is a tpe and cannot be used as an expression

Furthermore when I type in Cache and press the "." for intellisense, the
only things that pop up are Equals, NoAbsoluteExpiration,
NoSliderExpiration, ReferenceEquals. No methods like Add, Insert and so
forth show up.

This drove me crazy for a while. Then I decided to try my cache code
directly in my ASP.NET code behind page and it worked just fine.

Eventually after mucking around for a while I found that if I did this in
my C# class file then things worked correctly:

public Cache MyCache;
MyCache = System.Web.HttpContext.Current.Cache;

and now I can use if (MyCache["sometoken''] == null) from my C# class and
it works just fine.

A couple of questions:

1) Is this really necessary or am I referencing or doing something in
correctly?

2) Is there another approach to accessing the Cache from a C# class called
by a ASP.NET code behind, or did I stumble upon the right approach by what
I am using here?

Hopefully I''ll get some clarification as to what is/was going on and why
this was necessary, and hopefully someone will find this post one day and
it will save them a lot of time.

Thanks,

Steve



be sure System.Web.dll is references by project your class is in, and to get
the current Cache object being used by the asp.net, call
System.Web.HttpContect.Current.Cache


Thanks. On a related note, I am now having difficulty getting to the Page
object within the same C# class that is called from my ASP.NET code behind.

For example, I can do the following just fine in my ASP.NET code behind:
Repeater1.ItemTemplate = Page.LoadTemplate("mytemplate.ascx");

However I want to be able to do the same thing from my C# class, but I
cannot figure out how to hook into the current Page instance from within my
C# class.

For example, when it came to the Cache object, I figured out that I had to
use System.Web.HttpContext.Current.Cache to hook into the Cach object
instance. I assume I need to do something similar to get at the current
Page object?.

I''ve been poking around but there is no System.Web.HttpContext.Current.Page
object and I can not figure out how to tap into the current Page so that I
can do a Page.LoadTemplate from my C# class.

Can you please advise on what I am missing? Thanks!

Steve
"S. Justin Gengo" <sjgengo@[no_spam_please]aboutfortunate.com> wrote in
message news:uk**************@TK2MSFTNGP09.phx.gbl...

Steve,

That''s what you had to do. The Cache object is used by the .aspx page
natively and therefore the object has already been instantiated. In a
class file the Cache object is not already being used so you had to
explicitly create a holder for it and then get the cache object being used
on the page.

Short story, you''re doing it exactly right.

--
Sincerely,

S. Justin Gengo, MCP
Web Developer / Programmer

www.aboutfortunate.com



这篇关于Cache是​​一个tpe,不能用作表达式错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆