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 modify 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.
To start debugging, select F5, or choose the Debug Target button in the Standard toolbar, or choose the Start Debugging button in the Debug toolbar, or choose Debug > Start Debugging from the menu bar. The app starts and the debugger runs to the line of code where you set the breakpoint.
You can attach the Visual Studio debugger to a running process on a local or remote computer. After the process is running, select Debug > Attach to Process or press Ctrl+Alt+p in Visual Studio, and use the Attach to Process dialog to attach the debugger to the process.
To set a breakpoint in source code: Click in the far left margin next to a line of code. You can also select the line and press F9, select Debug > Toggle Breakpoint, or right-click and select Breakpoint > Insert breakpoint. The breakpoint appears as a red dot in the left margin.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With