简体   繁体   English

在Visual Studio中调试时迭代可枚举对象

[英]Iterate enumerable object while debugging in Visual Studio

Is it possible to iterate collection and list only filtered object information while debugging in Visual Studio? 在Visual Studio中进行调试时,是否可以迭代集合并仅列出过滤的对象信息? I'd use Immediate window for that, but although it allows to execute methods on objects, it seems not to allow execute custom loop statements. 我会使用立即窗口,虽然它允许在对象上执行方法,但似乎不允许执行自定义循环语句。

Simplest example in ASP.NET: ASP.NET中最简单的例子:

this.Page.Validate();
if (!this.Page.IsValid())
{
  // breakpoint here
}

How can we iterate Page.Validators collection and find those which are invalid + output their information at that breakpoint? 我们如何迭代Page.Validators集合并找到那些无效的+在该断点处输出它们的信息? (this is not the main question, it's just a sample) (这不是主要问题,只是一个样本)

If it's not possible to do it straight forward, do you have any workarounds for this? 如果不能直接做到这一点,你有解决办法吗? Workarounds which would not involve code modification, just writing code in Immediate window or some Watch expression. 不涉及代码修改的解决方法,只需在立即窗口或某些Watch表达式中编写代码。

While googling I've found just one workaround quoted here (although could not find the original): 谷歌搜索我发现这里只引用一个解决方法(虽然找不到原文):

"Add a debug method to your code that does something like iterate through all the objects in a collection. Then you can call that method from the immediate window while in debug mode and it will enumerate various things for you. Think of it like a command-line-debugger-helper. You can write as many of those as you like." “在你的代码中添加一个调试方法,它可以像迭代集合中的所有对象一样。然后你可以在调试模式下从即时窗口调用该方法,它会为你枚举各种各样的东西。把它想象成一个命令-line-debugger-helper。您可以根据需要编写尽可能多的内容。“

But it's still a workaround. 但它仍然是一种解决方法。 I image it should be doable without too much hacking and more importantly without modifying code. 我认为它应该是可行的,没有太多的黑客攻击,更重要的是没有修改代码。 Of course it should be possible to do some kind of collection transformations in one statement. 当然,应该可以在一个语句中进行某种集合转换。

And let's stick to non-generic collections. 让我们坚持非泛型集合。 Also Immediate window seems not to accept lambda expressions (got an error when tried: "Expression cannot contain lambda expressions") 立即窗口似乎不接受lambda表达式(尝试时出错:“表达式不能包含lambda表达式”)

You could try using the immediate window and a LINQ-to-objects call. 您可以尝试使用立即窗口和LINQ到对象调用。

Contrived example: 举例:

 pages.Where((x) =>
 {
    if (x.IsValid)
    {
        Debugger.Break();
        return true;
    }
    return false;
 });

Update: Apparently, this won't work as immediate window doesn't allow lambdas. 更新:显然,这不起作用,因为立即窗口不允许lambdas。 However, if you implement the lambda as a debug only method you could do this. 但是,如果将lambda实现为仅调试方法,则可以执行此操作。

[Conditional("DEBUG")]
static bool BreakpointPredicate(YourItemType x)
{
    if (x.IsValid)
    {
        Debugger.Break()
        return true;
    }
    return false;
}

And then just put a call to Where in the immediate window: 然后只需在即时窗口中调用Where

pages.Where(BreakPointPredicate);

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

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