2011-07-03 5 views
5

プロセス内の各AppDomain用に作成されたパブリック静的変数のコピーは1つだけですか、それともプロセス全体のコピーは1つだけですか?つまり、あるAppDomain内から静的変数の値を変更すると、同じプロセス内の別のAppDomain内の同じ静的変数の値に影響しますか?.Netでは、AppDomainまたはプロセス全体に限定されたパブリック静的変数の「静的性」ですか?

答えて

10

この例により証明として、アプリケーションドメインごとである:

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

ありがとうございました。最高の答え! – Harindaka

+1

サンプルプログラムを実行しようとすると、メッセージでTypeLoadExceptionが発生します。アセンブリ 'ConsoleApplication1、Version = 1.0.0.0、Culture = neutral、PublicKeyToken = null'から 'Test'タイプを読み込めませんでした。 HResultは80131522です。 – DWright

0

それはアプリケーションドメインに制限され、換言すれば、変数は、各アプリケーションドメイン内の別の値として存在します。

関連する問題