In .Net is the 'Staticness' of a public static variable limited to an AppDomain or the whole process?
是为进程中的每个AppDomain创建一个公共静态变量的副本,还是只为整个进程创建一个副本?换句话说,如果我从一个AppDomain中更改静态变量的值,它会影响同一进程中另一个AppDomain中相同静态变量的值吗?
如本例所示,它是按应用程序域划分的:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | public class Foo { public static string Bar { get; set; } } public class Test { public Test() { Console.WriteLine("Second AppDomain: {0}", Foo.Bar); } } class Program { static void Main() { // Set some value in the main appdomain Foo.Bar ="bar"; Console.WriteLine("Main AppDomain: {0}", Foo.Bar); // create a second domain var domain = AppDomain.CreateDomain("SecondAppDomain"); // instantiate the Test class in the second domain // the constructor of the Test class will print the value // of Foo.Bar inside this second domain and it will be null domain.CreateInstance(Assembly.GetExecutingAssembly().FullName,"Test"); } } |
它仅限于AppDomain,换句话说,变量作为单独的值存在于每个AppDomain中。