在asp.net中使用静态对象

fAR*_*cRY 2 c# asp.net static

我在我的asp.net页面中使用了followign对象

private static Dictionary<string, List<Guid>> OpenNodes = new Dictionary<string,    List<Guid>>();  
//Page start
if(!OpenNodes.ContainsKey(Session.SessionID))  
{  
    List<Guid> list = new List<Guid>();  
    OpenNodes.Add(Session.SessionID, list);  
}

//User clicked on a node
Guid id = new Guid(e.Node.Value);  
tmpList = OpenNodes[Session.SessionID];  
tmpList.Add(id);  
OpenNodes[Session.SessionID] = tmpList;  
Run Code Online (Sandbox Code Playgroud)

它是否具有良好的吸引力还是有类似的"更好"的方法来实现同样的目标?

Meh*_*ari 7

您不应该Session使用静态字段替换.Session更灵活,更不容易出错.您可以轻松地使它在Web场中工作(您不能使用静态字段).您可以考虑用Application静态字段替换一些变量.重要的是要知道静态字段不提供开箱即用的任何线程安全机制,您应该适当地手动控制(锁定)它们.