This question is a bit old, but hopefully my solution (which relies on .NET 4+) will help someone.
By creating a proxy server as follows:
ProxyGenerator generator = new ProxyGenerator(); MyClass proxy = generator.CreateClassProxyWithTarget(underlying, new MyInterceptor(this));
I managed to get the basic goal in the following way:
internal static TType UnwrapProxy<TType>(TType proxy) { if (!ProxyUtil.IsProxy(proxy)) return proxy; try { dynamic dynamicProxy = proxy; return dynamicProxy.__target; } catch (RuntimeBinderException) { return proxy; } }
It relies on Castle's internal implementation β that is, the generated proxy has the __target member. It's nice and self-sufficient, and with the support of a unit test or two, we have to catch any changes if a later version of Castle breaks this. It uses Castle v3.2.0.0.
source share