dotnetdev dotnetdev - 1 month ago 5
C# Question

Marshal by bleed/reference/value?

I have heard about marshal by reference, marshal by bleed and marshal by value. What exactly are the differences between these 3? I know that these are used when transporting data across appdomains/serialization but not much more.


The different forms of marshaling are used to describe how objects behave when they are passed between AppDomain instances via normal function calls. An AppDomain is often known as a light weight process and provides an isolated container for managed objects to run in. Here's a quick breakdown of the different types

Marshal By Reference

All types which derive from MarshalByRefObject will marshal by reference. These object instances do not travel between AppDomain instances. They are allocated in a specific AppDomain and do not leave it.

When a reference to a MarshalByRefObject is passed across an AppDomain boundary a proxy is created in the target AppDomain. This proxy can be used to manipulate the object in the original AppDomain but the object itself is not directly accessible.

Marshal By Value

Essentially the opposite of MarshalByRefObject. When these values are passed across AppDomain boundaries they are serialized via binary serialization and deserialized in the target AppDomain instance. The result is two, hopefully, independent values. One in each domain.

Marshal By Bleed

Certain classes of types are known as Domain Neutral. In particular string, Type and other reflection members. These objects do not live in a particular AppDomain and references to them can be freely shared between them. They are similar to marshal by reference in that duplicates are not created but proxies are not created either. Instead the direct reference is shared between AppDomain instances.

You should take a look at Joe Duffy's blog entry on the subject