Search code examples
.netvisual-studioreflectionremoting

In .NET Remoting how do you tell if an object is used as a remotable object?


I know that .NET Remoting has been replaced by WCF, but this is more of an academic question than anything else.

Suppose that you have a remotable class that is defined as follows:

public class MyObject : MarshalByRefObject
{
}

And suppose that you have client code that instantiates a remotable object of type MyObject but does not use it as a remotable object:

public static void Main(String[] args)
{
   MyObject mo = new MyObject();
}

Now, suppose that I want to programmatically tell the type of mo. There are, as far as I know, two ways to do it:

Console.WriteLine(mo.GetType().ToString());//MyObject

or

Console.WriteLine(GetType(mo).ToString()); //MyObject

where GetType() is defined as follows:

static Type GetType<T>(T t)
{
   return typeof(T);
}

In either case the compile time type and the runtime type of mo is MyObject.

But now suppose that I have client code that actually uses an instance of MyObject as a remotable object:

public static void Main(String[] args)
{
   System.Runtime.Remoting.RemotingConfiguration.Configure("Client.exe.config");
   MyObject mo = new MyObject();
}

If I want to know the type of mo I can use any of the two methods I used above, and get the same results. However, when I place a breakpoint and hit F5, Visual Studio tells me that the type of mo is actually MyObject {System.Runtime.Remoting.Proxies.__TransparentProxy}

What I want to know is how to tell, programmatically, that the underlying type of mo is __TransparentProxy.


Solution

  • bool isProxy = System.Runtime.Remoting.RemotingServices.IsTransparentProxy(obj);