Question

I have a section of code which is passing an object across AppDomains and to make debugging easier I want to get rid of the TransparentProxy.

In the course of writing this sample, I discovered how to do it, but I have two very similar snippets of code which behave differently and I am not sure why.

I know the actuals values are correct, so this is just to aid debugging.

In the example below. I have a class Data which is initialised in the default domain and passed to Process which is running in another domain. If I attempt to clone the data structure using the static method it works, but using the instance method does and I don't quite understand why.

Can anyone explain?

using System;

class Program
{
    static void Main(string[] args)
    {
        AppDomain otherDomain = AppDomain.CreateDomain("Test");

        var otherType = typeof(Process);
        var process = otherDomain.CreateInstanceAndUnwrap(
                 otherType.Assembly.FullName, otherType.FullName) as Process;

        Data d = new Data() { Info = "Hello" };
        process.SetData(d);
    }
}

public class Process : MarshalByRefObject
{

    public void SetData(Data data)
    {
        Data data1 = Data.Clone(data);
        Data data2 = data.Clone();

        Console.WriteLine(data1.Info); 
           // Debugger shows data1.Info as Hello

        Console.WriteLine(data2.Info); 
           // Outputs Hello, but Debugger shows data2 as     
           // System.Runtime.Remoting.Proxies.__TransparentProxy
    }

}

public class Data : MarshalByRefObject
{
    public string Info { get; set; }

    public Data Clone()
    {
        return Data.Clone(this);
    }

    public static Data Clone(Data old)
    {
        var clone = new Data();
        clone.Info = old.Info;
        return clone;
    }
}
Was it helpful?

Solution

When you call the static method from the other appdomain, the method is invoked in that appdomain and the new Data object is created in that appdomain. OTOH when you call the instance method, the call is marshaled to the object's original appdomain, so the clone Data object is created in the original appdomain. Then it is marshaled back to the other appdomain as a transparent proxy and appears there as a retern value.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top