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