静态变量实例和AppDomains,发生了什么?

我有

public static class A { public static string ConnString; } [Serializable] public class Test{ // Accesing A's field; public string ConnString{get{return A.ConnString;}set{A.ConnString=value;}} } void Main() { A.ConnString = "InitialString"; // I set A.ConnString in the current domain var newDomain = AppDomain.CreateDomain("DomNew"); Test TObj = newDomain.CreateInstanceAndUnwrap(typeof(Test).Assembly.FullName, typeof(Test).FullName) as Test ; TObj.ConnString = "NewDomainString"; // It is supposed to set A.ConnString in the newDomain aka a different instance of A.ConnString // Here it is supposed to print two different values Console.WriteLine(A.ConnString); // "InitialString" Console.WriteLine(TObj.ConnString); // "NewDomainString" } 

但不是! 两个WriteLines,打印出相同的值“NewDomainString”! 为什么???

这段代码

 TObj.ConnString = "NewDomainString" 

应该更改新创建的域中的字符串,但似乎它们都引用同一个实例!

为什么,这里发生了什么?

只有两种方法可以从另一个AppDomain访问一个类 – 一个是类[Serializable] ,正如您的Test类一样,另一个是该类是否inheritance自MarshalByRefObject 。 因为您的类是Serializable,所以会为每个跨AppDomain调用创建一个副本。 所以主要appdomain在你打电话时得到的Test ……

 Test TObj = newDomain.CreateInstanceAndUnwrap(typeof(Test).Assembly.FullName, typeof(Test).FullName) as Test; 

实际上不是在“DomNew”AppDomain中创建的Test实例 – 它是“main”AppDomain的本地副本,因此引用“main”AppDomain中的静态变量。

如果希望Test显示您期望的行为,请使其inheritanceMarshalByRefObject而不是Serializable。

您将Test类标记为Serializable。 这是错的。 你应该从MarshalByRef派生出来。 否则TObj将只是当前AppDomain中的本地副本。