Serdecznie Pozdrawiam,
Michał Komorowski
The blog about programming, working in IT and not only


\Tree[.{L0 - Root}
[.{L1 - Left child}
[.{L2 - Left child} ]
[.{L2 - Right child} ]]
[.{L1 - Right child} ]]
Efekt końcowy wygląda natomiast następująco:
public interface ITest
{
int Value { get; set; }
void Start();
}
public class MarshalByRefObjectClass : MarshalByRefObject, ITest
{
public int Value { get; set; }
public void Start()
{
Console.WriteLine(AppDomain.CurrentDomain.FriendlyName);
Value++;
}
}
[Serializable]
public class MarshalByValueClass : ITest
{
public int Value { get; set; }
public void Start()
{
Console.WriteLine(AppDomain.CurrentDomain.FriendlyName);
Value++;
}
}
Mamy również następujący kod, w którym testuję jak zachowuja się:
private static void Test(AppDomain app, ITest test, bool doCallBack)
{
if (doCallBack)
app.DoCallBack(test.Start);
else
test.Start();
Console.WriteLine(test.Value);
}
A to właściwy test, w którym najpierw tworzę obiekty przekazywane przez wartość/referencję lokalnie i w nowej domenie. Następnie wywołuję dla nich metodę Start i odczytuję właściwość Value.
var app = AppDomain.CreateDomain("TestDomain");
var asm = Assembly.GetExecutingAssembly();
var byRef = new MarshalByRefObjectClass();
var byRef1 = new MarshalByRefObjectClass();
var byRef2 = (MarshalByRefObjectClass)app.CreateInstanceFromAndUnwrap(asm.CodeBase, typeof(MarshalByRefObjectClass).FullName);
var byRef3 = (MarshalByRefObjectClass)app.CreateInstanceFromAndUnwrap(asm.CodeBase, typeof(MarshalByRefObjectClass).FullName);
var byValue = new MarshalByValueClass();
var byValue1 = new MarshalByValueClass();
var byValue2 = (MarshalByValueClass)app.CreateInstanceFromAndUnwrap(asm.CodeBase, typeof(MarshalByValueClass).FullName);
var byValue3 = (MarshalByValueClass)app.CreateInstanceFromAndUnwrap(asm.CodeBase, typeof(MarshalByValueClass).FullName);
Test(app, byRef, true);
Test(app, byRef1, false);
Test(app, byRef2, true);
Test(app, byRef3, false);
Test(app, byValue, true);
Test(app, byValue1, false);
Test(app, byValue2, true);
Test(app, byValue3, false);
Pytanie brzmi co zostanie wypisane na ekranie?
Pokaż/Ukryj odpowiedź
Sandbox.vshost.exe 1 Sandbox.vshost.exe 1 TestDomain 1 TestDomain 1 TestDomain 0 Sandbox.vshost.exe 1 TestDomain 0 Sandbox.vshost.exe 1