Is it possible to iterate collection and list only filtered object information while debugging in 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:
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? (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.
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."
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")
You could try using the immediate window and a LINQ-to-objects call.
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. However, if you implement the lambda as a debug only method you could do this.
[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:
pages.Where(BreakPointPredicate);
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