gpt4 book ai didi

.net - 在 .Net 中,公共(public)静态变量的 'Staticness' 是否仅限于 AppDomain 或整个过程?

转载 作者:行者123 更新时间:2023-12-04 17:27:16 24 4
gpt4 key购买 nike

是否为进程中的每个 AppDomain 创建了一个公共(public)静态变量的副本,或者它只是整个进程的一个副本?换句话说,如果我从一个 AppDomain 中更改静态变量的值,是否会影响同一进程中另一个 AppDomain 中相同静态变量的值?

最佳答案

正如此示例所证明的,它是每个应用程序域:

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");
}
}

关于.net - 在 .Net 中,公共(public)静态变量的 'Staticness' 是否仅限于 AppDomain 或整个过程?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6562162/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com