每个用户的静态对象是唯一的吗?
我有一个 .net 应用程序(c#),其内容如下
public partial class _Default : System.Web.UI.Page
{
#region initial variables setup
private static exam theExam;
#endregion
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
{
string userid = Request.Querystring["user"].ToString();
theExam = new exam(userid, "some values");
}
}
// rest of code.
现在我的问题是,如果用户 105 登录考试实例,则会创建考试并将其分配给顶部的静态声明。如果用户 204 然后从另一台计算机登录,即使在用户 105 的计算机上,顶部的静态对象是否也会获得 204 的值?
I have a .net application (c#) that goes something like this
public partial class _Default : System.Web.UI.Page
{
#region initial variables setup
private static exam theExam;
#endregion
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
{
string userid = Request.Querystring["user"].ToString();
theExam = new exam(userid, "some values");
}
}
// rest of code.
Now my question is, if user 105 logs in an instance of exam theExam is created and assign to the static declaration on top. If user 204 then logs in from a different computer, does the static object at the top gets the value of 204 even on user's 105's computer?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
不,静态对象对于每个登录的人来说都是同一个实例。当然,该对象并不存在于 105 的计算机上,而只是存在于 Web 服务器上。
No, the static object is the same instance for everyone logged on. Also the object doesn't live on 105's computer but just on the web server ofcourse.
静态变量的生命周期和用户会话是非常不同的概念。静态变量的生命周期由 CLR 定义,本质上可归结为以下 2 条规则
我发现很难在不明确唯一的情况下编写第二条规则。本质上,
MyType
和MyType
各自具有不同的静态变量。而MyType
和MyType
共享同一个。用户对 Web 服务器的访问不会影响其中任何一个。
如果您想要每个用户的数据,请使用
Session
来存储数据。Lifetime of static variables and user sessions are very different concepts. Static variables have a lifetime defined by the CLR and essentially boils down to the following 2 rules
AppDomain
I'm finding it hard to write the second rule without it being ambiguous as to unique. Essentially
MyType<int>
andMyType<string>
each have different static variables. WhileMyType<int>
andMyType<int>
share the same one.User's acess to a web server don't affect either of these.
If you want to have per user data then use the
Session
to store the data.简短的回答:是的,静态字段对于 AppDomain 来说是全局的,因此为一个用户执行此操作将会占用另一用户的数据。
您可能想考虑使用会话存储,它的范围是每个用户的,例如
Short answer: yes, the static field is global to the AppDomain, so doing this for one user will step on the data for another user.
You probably want to look into using Session storage instead, which is scoped per-user, e.g.
每个 AppDomain 有一个静态对象的“实例”。所以你的问题的答案是肯定的。由于您在用户 204 登录时覆盖了该变量,因此用户 105 也会出现相同的值。
一些一般建议
Session["exam"] = currentUser.Exam;
There is one "instance" of a static object per AppDomain. So the answer to your question is yes. Since you overwrite the variable when user 204 logs in, the same value will appear for user 105, too.
Some general advices
Session["exam"] = currentUser.Exam;
.Net 中还有一个 [ThreadStatic] 属性,可以为每个线程生成一个静态实例。
http://msdn.microsoft.com/en- us/library/system.threadstaticattribute(VS.71).aspx
There's also a [ThreadStatic] attribute in .Net that will make one static instance per thread.
http://msdn.microsoft.com/en-us/library/system.threadstaticattribute(VS.71).aspx