How to debug a __transparentProxy instance in VisualStudio 2008?
I'm currently working on a debugging topic to improve the debugging to __TransparentProxy
instance resolved from Unity's TransparentProxyInterceptor
.
The common debugging scenario is that while the program is breaking, I want to see the public member value or call the method on the proxy-wrapped instance in either Watch window or Immediate window in VS2008 IDE.
Here comes the problem: While accessing the public property or calling method on my proxy-wrapped instance, I always encounter the exception message which shows in the Watch / Immediate window, says
'Cannot obtain fields or call methods on the instance of type 'MyDomainObject' because it is a proxy to a remote object.'
I've dug into the threads on the web, and found that the cause of this exception is due to the internal reflection behavior of .NET __transparentProxy
. The __transparentProxy
instance can't access the corresponding property/method on the RealProxy
instance underlying in the __transparentProxy
instance itself.
AFAIK, there're two ways (without any design or assistance from external tools) to obtain the value I want. One is keep unfolding and unfolding the private member value in Watch window, and after several click I can , finally, access the proxy-free original instance, on which I can do whatever I want to. The other way is much faster but still take a little effort each time you want to access the original unproxied instance: RemotingServices.GetRealProxy(myProxiedObject).Target
Either way is workable but takes some efforts to get the instance I want, and while the proxied instance I want resides in a deep hierarchy, it's awfully tedious to keep unfolding or writing RemotingServices.GetRealProxy(myProxiedObject).Target
(loop this) .
I've come up with two solutions, one is try to get some help from DebuggerTypeProxyAttribute
in .NET, but seems failed to do so because I have to append this attribute to the RealProxy
-derived class and I really don't want to mod开发者_如何学编程ify the source code of InterceptingRealProxy
class in Unity.
The other way seems feasible but twist my class design a little. I add a protected readonly
property called _rawInstance
in my proxy-wrapped target instance's base class, thus I can see _rawInstance
in my watch window which leads me directly to my original unwrapped object. The code may look like this:
public class MyDomainBase : MarshalByRefObject
{
protected MyDomainBase _rawInstance
{
get{ return this; }
}
(... some members, ignored)
}
I'm wondering if there's any better solution to this issue? Any information or tips would be very appreciated.
You might want to write a visualizer for that. It's a plugin for Visual Studio to "visualize" any watch value and you can do whatever you want to do in there, instead of in your actual project code.
精彩评论