Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I tell the CLR to marshal immutable objects between AppDomains by reference?

When marshaling objects between AppDomains in .NET the CLR will either serialize the object (if it has the Serializable attribute) or it will generate a proxy (if it inherits from MarshalByRef)

With strings however the CLR will just pass the reference to the string object into the new AppDomain. The CLR still ensures integrity since .NET strings are immutable and any change by the second AppDomain to the string will not effect the original object.

Which brings me to my question: is there a way to tell the CLR that my custom type is immutable and when used in remoting it should just pass the reference to the object as it does with the string class?

like image 870
Yona Avatar asked May 05 '09 13:05

Yona


1 Answers

Marshalling is actually fairly tricky.

The behaviour you are describing is called "marshal-by-bleed", the runtime uses it to marshal strings (sometimes) and marshal System.Threading.Thread ALWAYS.

As far as I can tell you have no control over this (its mentioned in the article that you can define custom marshalling behaviour but I can not find any documentation on it), you could potentially pass an IntPtr around and use unsafe code to simulate this, but it smells like a huge hack to me.

like image 130
Sam Saffron Avatar answered Oct 21 '22 18:10

Sam Saffron