如何在 VisualStudio 2008 中调试 __transparentProxy 实例?
我目前正在研究一个调试主题,以改进对从 Unity 的 TransparentProxyInterceptor
解析的 __TransparentProxy
实例的调试。
常见的调试场景是,当程序中断时,我想在 VS2008 IDE 的“监视”窗口或“立即”窗口中查看公共成员值或调用代理包装实例上的方法。
问题来了:在访问代理包装实例上的公共属性或调用方法时,我总是遇到“监视/立即”窗口中显示的异常消息,
'无法在“MyDomainObject”类型的实例上获取字段或调用方法,因为它是远程对象的代理。
我深入研究了网络上的线程,发现原因是此异常是由于 .NET __transparentProxy
的内部反射行为造成的。 __transparentProxy
实例无法访问 __transparentProxy
实例本身底层的 RealProxy
实例上相应的属性/方法。
AFAIK,有两种方法(无需任何设计或外部工具的帮助)来获得我想要的价值。一是在Watch窗口中不断地展开和展开私有成员值,点击几次后我终于可以访问无代理的原始实例,在该实例上我可以做任何我想做的事情。另一种方法要快得多,但每次要访问原始的未代理实例时仍然需要花费一些精力:RemotingServices.GetRealProxy(myProxiedObject).Target
两种方法都是可行的,但需要一些努力才能获取实例我想要,虽然我想要的代理实例驻留在很深的层次结构中,但继续展开或编写 RemotingServices.GetRealProxy(myProxiedObject).Target
(loop this) 是非常乏味的。
我想出了两种解决方案,一种是尝试从 .NET 中的 DebuggerTypeProxyAttribute 获得一些帮助,但似乎未能这样做,因为我必须将此属性附加到 RealProxy
code> 派生类,我真的不想修改 Unity 中 InterceptingRealProxy
类的源代码。
另一种方法似乎可行,但稍微扭曲了我的班级设计。我在代理包装的目标实例的基类中添加了一个名为 _rawInstance
的 protected readonly
属性,因此我可以在监视窗口中看到 _rawInstance
,它会导致我直接到我原来打开的物体。代码可能如下所示:
public class MyDomainBase : MarshalByRefObject
{
protected MyDomainBase _rawInstance
{
get{ return this; }
}
(... some members, ignored)
}
我想知道这个问题是否有更好的解决方案?任何信息或提示将不胜感激。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可能想为此编写一个可视化工具。它是 Visual Studio 的一个插件,用于“可视化”任何监视值,您可以在其中执行任何您想做的操作,而不是在实际的项目代码中执行。
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.