在应用程序状态(ASP.NET)中存储类

问题描述:

假设我希望将以下实例存储在应用程序状态中,以便经常访问。

Let's say I wish to store an instance of the following in Application State, to be accessed very often.

public class Example {
  public string A;
  public string B;
  public bool C;
  public int D;
  // ...
}

我无法决定是否将整个类存储为 Application [ Example] ,或将其属性分别存储为 Application [ ExampleA] 等。

I can't decide whether to store the whole class together as Application["Example"], or to store its properties individually as Application["ExampleA"] etc.

我的想法是(((Example)Application [ Example]])。A 必须将整个类复制到内存中才可以访问一个属性-是吗?还是我弄错了?

My thinking is that ((Example)Application["Example"]).A might have to copy the whole class into memory just to access one property - is that right? Or am I mistaken?

我将使用静态全局变量,性能稍好,输入安全,并使代码更容易读书。有关更多信息,请参阅...

I would use a static global variable, slightly better performance, type safe and will make your code easier to read. For more info see...

ASP.NET应用程序状态与静态对象