I have
public static class A
{
public static string ConnString;
}
[Serializable]
public class Test{
// Accesing A\'s field;
public string ConnStrin
-
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.
讨论(0)
-
You marked your Test class as Serializable. This is wrong. You should have derived from MarshalByRef. Otherwise TObj will just be a local copy in the current AppDomain.
讨论(0)
- 热议问题