在净是局限于一个AppDomain或整个过程一个公共静态变量的“Staticness”? [英] In .Net is the 'Staticness' of a public static variable limited to an AppDomain or the whole process?

查看:214
本文介绍了在净是局限于一个AppDomain或整个过程一个公共静态变量的“Staticness”?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于在一个进程中的每个应用程序域创建一个公共静态变量的一个副本,或只是一个副本的整个过程?换句话说,如果我从改变一个静态变量的值在一个AppDomain中,是否会影响相同的静态变量的值在另一个的AppDomain在同一个进程?

Is one copy of a public static variable created for each AppDomain in a process or is it just one copy for the whole process? In other words if I change the value of a static variable from within one AppDomain, will it affect the value of the same static variable within another AppDomain in the same process?

推荐答案

这是每个应用程序域证明了这个例子:

It is per application domain as proven by this example:

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或整个过程一个公共静态变量的“Staticness”?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆