简体   繁体   English

每个进程只有一个静态变量实例吗?

[英]Is there only one instance of a static variable per process?

If I have the following class: 如果我有以下课程:

public class MyClass { public static int MyStaticInt = 0; }

If in the one solution I refer to MyNameSpace.MyClass.MyStaticInt in two different assemblies, am I referring to the same variable? 如果在一个解决方案中我在两个不同的程序MyNameSpace.MyClass.MyStaticInt引用MyNameSpace.MyClass.MyStaticInt ,我指的是同一个变量吗?

Static state is scoped per AppDomain by default and can be configured to be by thread if you use the ThreadStatic attribute. 默认情况下,静态状态是每个AppDomain的范围,如果使用ThreadStatic属性,则可以通过线程配置静态。

This means that your assumption is valid if the assemblies are running in the same process and the process has only one application domain. 这意味着如果程序集在同一进程中运行且该进程只有一个应用程序域,则您的假设是有效的。

static can mean several things depending on context. static可能意味着几件事情取决于上下文。

  • By default, you get one instance of the value per AppDomain . 默认情况下,每个AppDomain获取一个值实例。
  • If decorated with the ThreadStatic attribute, you get one instance of the value per thread. 如果使用ThreadStatic属性修饰,则每个线程获得一个值的实例。
  • If contained within a generic class, you get one instance of the value per concrete type. 如果包含在泛型类中,则每个具体类型获得一个值的实例。

For your example code, the first condition appears to be the case. 对于您的示例代码,第一个条件似乎是这种情况。 In all cases, the specific assembly that the data is defined in does not make any difference. 在所有情况下,定义数据的特定程序集没有任何区别。

Yes, there is only one instance per process per class. 是的,每个进程每个进程只有一个实例。

A small caveat to this is when you have generic classes where you have one instance of the variable per instance of the generic class. 一个小小的警告是,当你有泛型类时,每个泛型类的实例都有一个变量实例。 Ie you would have one instance for MyGenericClass and one for MyGenericClass. 即你有一个MyGenericClass实例和一个MyGenericClass实例。

EDIT 编辑

In fact there is one instance per AppDomain, so you can create multiple copies by creating mulitple copies of the AppDomain yourself. 实际上每个AppDomain有一个实例,因此您可以通过自己创建AppDomain的多个副本来创建多个副本。

,MyClass.MystaticInt只是MyClass类的本地。

I've tested the static instance in a " custom " assembly, which is loaded from two locations: the main program (3rd party) and my " home-made " plugin that is loaded also by the main program. 主程序还加载了主程序 (第三方)和我的“ 自制 ”插件:我在“ 自定义 ”组件,这是从两个位置加载测试的静态实例。 I've checked the AppDomain - it is exactly the same when the " custom " assembly is loaded from both places, alas the static instance of an object in a " custom " assembly is not the same. 我已经检查了AppDomain - 当从两个地方加载“ 自定义 ”程序集时它完全相同,唉,“ 自定义 ”程序集中的对象的静态实例是不一样的。 Therefore I have to conclude that static instance has the aforementioned single value per AppDomain per loaded assembly, if the assembly is loaded again, then it will not be the same. 因此,我必须得出结论,静态实例每个加载程序集的每个AppDomain都有上述单个值,如果再次加载程序集,那么它将不相同。

class variable are static. class变量是静态的。 there is only one occurance of a class variable per jvm per classloader. 每个类加载器每个jvm只有一个类变量出现。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM