Various sources explain that
When an object derives form MarshalByRefObject, an object reference will be passed from one application domain to another rather than the object itself. When an object is marked with [Serializable], the object will be automatically serialized, transported from one application domain to another and then deserialized to produce an exact copy of the object in the second application domain. Note then that while MarshalByRefObject passes a reference, [Serializable] causes the object to be copied. [source]
I'm designing my first app that uses AppDomains and I'm wondering what happens when you place references to MarshalByRefObjects
inside serializable objects that do not implement MarshalByRefObject, because so far I can't find any documentation on the subject.
For example, what happens if I try to return a List<MBR>
where MBR : MarshalByRefObject
across an AppDomain boundary? Do I get a copy of the List<MBR>
where each MBR
is a TransparentProxy
to the original object? And is there any documentation about the technical details of mixing the two mechanisms?
I just did a quick test with
List<MBR>
and it seems to work as I had hoped:The output is
X=42, Count=1
, and the debugger shows that theList<MBR>
contains a__TransparentProxy
. So clearly, theMarshalByRefObject
is successfully marshaled by reference inside another object that was marshaled by value.I would still like to see documentation or technical details if anyone can find some.
For anyone who is curious, I wrote this handy-dandy sandbox AppDomainStarter:
It is my understanding that only the top-level object that is passed may be MBR. In your scenario, since List is not MBR, when it is passed over the boundary, you will receive serialized copies.
This section in the MSDN documentation explains this behavior:
So, since the class (List) that is passed is not MBR, it will be serialized, along with its contents.
Also, while not directly applicable to the question, the following behavior is very important to note: