简体   繁体   English

如何以编程方式在调试器中添加监视

[英]How to programatically add watch in debugger

You can programatically add break (Debugger.Break()) and I do this in some places where it means something has gone wrong (instead of, or in addition to throwing exceptions). 您可以以编程方式添加break(Debugger.Break()),我在某些地方发生了问题(而不是抛出异常,或抛出异常)进行了此操作。

In some cases, I would like to programatically add watch for certain variables. 在某些情况下,我想以编程方式添加对某些变量的监视。 Can this be done somehow? 可以以某种方式做到这一点吗? At present the best I can come up with is 目前我能想到的最好的是

object foo = someObjectIWantToWatch;

This way, I at least have a reference to the object I want to watch in code, so I can mouseover that object to see its contents. 这样,我至少可以在代码中引用要查看的对象,因此可以将鼠标悬停在该对象上以查看其内容。 But this is obviously a hack and I'm hoping to find a better way. 但这显然是一个hack,我希望找到一种更好的方法。

Question edited: 问题已编辑:

I have a big application, with asynchronous threads driven by various system events, but when something goes wrong, you want to browse all the data structures in the whole application, looking for any clues what went wrong. 我有一个很大的应用程序,它的异步线程由各种系统事件驱动,但是当出现问题时,您希望浏览整个应用程序中的所有数据结构,以查找发生问题的任何线索。 So at the top level, where the application launched from (Main()) creates an object which will contain everything else hierarchically, and Main() goes into an infinite loop. 因此,在顶层,从(Main())启动的应用程序创建了一个对象,该对象将分层包含其他所有内容,并且Main()进入无限循环。

while (true)
{
    Thread.Sleep(100);
}

So if the application begins misbehaving for some reason, you can just put a breakpoint there, and voila. 因此,如果应用程序由于某种原因而开始出现异常,则可以在此放置一个断点,然后瞧。 You can browse all the data structures in the whole application. 您可以浏览整个应用程序中的所有数据结构。 But only if you have a reference to the top level container object. 但仅当您引用顶层容器对象时。 You can mouseover the object and browse that way - but it's more convenient to have the object appear in Autos or Watch. 您可以将鼠标悬停在对象上并以这种方式浏览-但是将对象显示在“自动”或“手表”中更为方便。 So I would like to do something like 所以我想做些类似的事情

Debugger.Watch(theTopLevelObject);

Yes I can browse to and find the top level object, and right-click it, and add watch. 是的,我可以浏览并找到顶层对象,然后右键单击它并添加监视。 But the actual declaration is nowhere nearby, hard to find when you're actually on the breakpoint. 但是实际的声明不在附近,很难真正断点处找到。

I know this is not the answer to your question, but I'd like to point out that you can do a Debug.WriteLine(someObjectIWantToWatch) , and look in the Output tab to see what the value was. 我知道这不是您问题的答案,但我想指出您可以执行Debug.WriteLine(someObjectIWantToWatch) ,然后在“输出”选项卡中查看该值是什么。 Also, could use a tool like DebugView to watch the Debug.WriteLine output without being in the debugger. 另外,可以使用像DebugView这样的工具来观看Debug.WriteLine输出,而无需进入调试器。

There may be an API in the Visual Studio Debugger Extensibility API or the Visual Studio SDK for adding Watch variables, but I didn't see one. Visual Studio调试器可扩展性APIVisual Studio SDK中可能有一个API用于添加Watch变量,但我没有看到。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM