What's the purpose for MarshalByRefObject?
4 Answers
Remoting; it means that between AppDomains or machines, rather than serialize and transfer the object, the object stays at one end and a proxy is created at the other. All method calls to the proxy are intercepted and the call is transmitted as RPC wiki, msdn, causing the method to execute on the other machine (typically serialising the arguments and return value).
Note: this can lead to unexpectedly "chatty" interfaces. The object/proxy/RPC approach is now less preferable to approaches with an explicit service boundary; Microsoft now recommends WCF wiki, msdn instead of remoting.

- 19,134
- 9
- 53
- 73

- 1,026,079
- 266
- 2,566
- 2,900
-
5@pavel - it is fairly complex, but at least it us clear where the expensive calls are. If you mean *bandwidth*, there are ways of mitigating that. – Marc Gravell Nov 28 '10 at 09:20
-
1Sometimes you do not *want* explicit boundaries, such as in scenarios listed for https://processdomain.codeplex.com/ Instead you want transparency. – Andrew Savinykh Oct 15 '14 at 22:10
-
1in c# all reference types are marshal by ref default or how to know it, soory if it's silly question, just new with this concept @MarcGravell – Ehsan Sajjad Dec 15 '15 at 17:06
-
1@Ehsan no, they are marshal-by-val by default. Marshalling only applies when crossing app-domain boundaries, though, so it is rare that it is even relevant. To be marshal-by-ref, it must inherit MashalByRefObject – Marc Gravell Dec 15 '15 at 18:51
Another important use of MarshalByRefObject
is for implementing AOP via remoting sink-chains.
If you have an object that derives from ContextBoundObject
(which itself derives from MarshalByRefObject
) you can instantiate it in a separate Context within the same AppDomain and have communications between objects automatically go through the Remoting proxy system - allowing you to plug custom sinks into the Remoting sink-chain.
This ultimately allows you to 'decorate' method calls to your objects and implement cross-cutting services, such as logging and security etc.

- 20,076
- 6
- 59
- 90
-
Just thinking on these -How could we decorate such method calls to MBR objects ? Are those decorations will cost in terms of performance ? – SO19 Apr 01 '18 at 03:52
it basic use is for support access of objects between two appdomains and these appdomains can be on the same computer or in the different computers via remoting.

- 17,262
- 5
- 38
- 63
Any object outside the application domain of the caller application should be considered as Remote Object. A Remote Object that should be derived from MarshalByRefObject
Class. Any object can be changed into a Remote Object by deriving it from MarshalByRefObject
. Objects without inheriting from MarshalByRefObject
are called Non-remotable Objects.

- 4,532
- 4
- 53
- 64

- 71
- 1
- 3