How do I detect if a MarshalByRefObject is local or remote?

1k Views Asked by At

In other words, how do I tell if I have a reference to a TransparentProxy or a local object?

2

There are 2 best solutions below

0
On

Take a look at the IsTransparentProxy method found in RemotingServices.

1
On

Try this fun little trick:

bool fIsTransparentProxy = ((myTransparentObject as MarshalByRefObject) == null);

When I attempted to convert a transparent proxy to MarshalByRefObject, it returned null. I tested this in VB, since that's where all my proxies are, but hopefully the same behavior holds true in C#.

Also note that Visual Studio debugger knows which is which, but I couldn't figure out how to get code that produces the same results (hovering over a TP object in VS shows System.Runtime.Remoting.Proxies.__TransparentProxy as the class type, but this is an internal sealed class).