Transparent proxy to original type

hungryMind picture hungryMind · Aug 14, 2011 · Viewed 14.6k times · Source

I have an run time object of type {System.Runtime.Remoting.Proxies.__TransparentProxy} which is created from an instance of class which is inherited from ContextBoundObject. This class raise an event to some other object, I need to convert this proxy object to original object. All objects are in default AppDomain on single system.

public abstract class ObjectBase : ContextBoundObject, IObjectBase
{
}

public IMessageSink GetObjectSink(MarshalByRefObject o, IMessageSink next)
        {
            _context = (ObjectBase)o;// as ObjectBase; does not give any error but type remains 
/// transparent proxy in VS watch window.
// no property to get the  underlying type of the proxy
             return _aspect;
        }

How to convert them into original object? Why proxy is cretaed if running on same memory

Answer

Lucero picture Lucero · Aug 15, 2011

You can get the RealProxy instance for the transarent proxy by calling MarshalServices.GetRealProxy(), but getting the server object reference is then harder because the default RealProxy only has non-public members exposing this reference (a protected method GetUnwrappedServer() and an internal property UnwrappedServerObject). You can either get to those if the RealProxy is implemented by yourself or via reflection (if you have enough trust to perform this).