2

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

Qwertie
  • 16,354
  • 20
  • 105
  • 148
  • 1
    Shouldn't your code really *know* a-priori whether you're on the local or remote side though? – Noldorin Jan 19 '12 at 23:01
  • Also, does `obj.GeType().IsMarshalByRef` or `obj.GetType().IsContextful` yield anything useful perchance? – Noldorin Jan 19 '12 at 23:15
  • I actually am debugging trouble with objects crossing the boundary unexpectedly (via serialization), so I wanted to write an Assert that a certain object is a TP. – Qwertie Jan 21 '12 at 12:06
  • Ah right, fair enough then. How about either of those solutions I just suggested though? – Noldorin Jan 21 '12 at 19:46
  • @Noldorin, I think if `obj` has type `X`, `GetType()` would return the same thing (`typeof(X)`) regardless of whether `obj` is local or remote. – Qwertie Jan 24 '12 at 00:57
  • You'd think that, but actually no; proxies are weird things. They appear the same for most purposes though. – Noldorin Jan 24 '12 at 10:54

2 Answers2

5

Take a look at the IsTransparentProxy method found in RemotingServices.

ahawker
  • 3,306
  • 24
  • 23
0

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).

competent_tech
  • 44,465
  • 11
  • 90
  • 113
  • Doesn't work for me. The cast succeeds when the object is a TP. All my objects are in the same process though (different AppDomain). – Qwertie Jan 19 '12 at 22:20