如何将Stack
对象添加到global.asax
中,以便整个应用程序都可以访问它?目前,我有:
void Application_Start(object sender, EventArgs e)
{
// Global vars
Application.Add("ArcadeTotalUniquePlayers", 0);
Application.Add("ArcadeTotalPlays", 0);
Stack<int> ArcadeLastPlayers = new Stack<int>(16);
前两个变量有效,但我不确定如何使Stack
全局访问。
您可以做:
Application.Add("ArcadeLastPlayers", new Stack<int>());
然后
((Stack<int>)Application["ArcadeLastPlayers"]).Pop();
或者您可以创建某种全局静态变量,因此您不必在每次需要检索它时都强制转换它:
namespace GlobalUtils {
public static class ArcadeData {
public static Stack<int> ArcadeLastPlayers = new Stack<int>();
}
}
然后
GlobalUtils.ArcadeData.ArcadeLastPlayers.Pop();
如果要使用HttpApplcationState,则应该能够执行以下操作:
Application.Add("ArcadeLastPlayers", new Stack<int>(16));
HttpApplicationState使用名称字符串和对象作为关联值。您需要做的就是在检索“ ArcadeLastPlayers”值时将其强制转换为堆栈。
使用singleton-将.cs
文件放置在app_code
文件夹中,鲍勃是您的阿姨。