Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Static Variable Instances and AppDomains, what is happening?

Tags:

I have

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

But NO! The two WriteLines, print out the same value "NewDomainString"! WHY???

this code

TObj.ConnString = "NewDomainString" 

is supposed to change the string in the newly created domain, but it seems they both refer to the same instance!

Why, what is happening here?

like image 261
Thanasis Ioannidis Avatar asked Mar 21 '12 14:03

Thanasis Ioannidis


People also ask

What happens to a static variable?

A static variable has a property to retain its value from it's previous scope. This means that it's value does not get re-initialized if the function in which it is declared gets called multiple times. If no value is assigned to a static variable, by default, it takes the value 0.

Are static variables shared between instances?

Static variables are shared among all instances of a class. Non static variables are specific to that instance of a class.

What are static variables used for?

Static variables are used to keep track of information that relates logically to an entire class, as opposed to information that varies from instance to instance.

What is the lifetime of static variable in C#?

Static variables have a program lifetime. It is created into static memory during compile time and it will be deleted after completion of the program. Hence the correct answer is till the end of the main program. The static variable default value is Zero.


1 Answers

There are only two ways for a class to be accessible from another AppDomain- one is is the class is [Serializable], as your Test class is, the other is if the class inherits from MarshalByRefObject. Because your class is Serializable, a copy of it is created for every cross-AppDomain call. So the Test that the main appdomain gets when you call...

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

is actually not the Test instance that was created in the "DomNew" AppDomain- it is a copy local to the "main" AppDomain, and therefore references the static variables from the "main" AppDomain.

If you want Test to exhibit the behavior that you expect, make it inherit from MarshalByRefObject instead of being Serializable.

like image 101
Chris Shain Avatar answered Sep 22 '22 18:09

Chris Shain