Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.NET: Attribute to force debugger to step into a property

Visual Studio has a debug setting called "Step over properties and operators (Managed only)". This setting is quite useful and I have it generally switched on.

Now every once in a while, there is a property setter/getter in a project, which is rather involved and while debugging I would like to be able to step into it. Is there a way to decorate this property with an attribute so that the debugger ignores the mentioned setting for the property and allows me to step into it?

Basically, it should do the inverse of the DebuggerStepThroughAttribute.

Or is there another way to achieve this? What I do currently is to set a breakpoint inside the property getter/setter before stepping over it, but that's not very handy since it requires me to add/remove the breakpoint each time I step through the specific code fragment.

Edit: the comments suggest refactoring. That doesn't really answer my question though, and is not necessary in my case. By "involved" I do not mean lots of code or some resource-intensive code. In my case, the property setter triggers a calculation inside the object (O(1) complexity, about two milliseconds). However, that calculation is not that obvious and every once in a while I want to step into the property setter by pressing the step into key.

like image 621
Andreas Avatar asked Feb 11 '14 21:02

Andreas


1 Answers

Well, to do this, you'd need a way to know whether the debugger is in run mode, or step through mode. Sadly, you don't get this information inside of your application.

So instead, I've written a simple macro:

dte.Debugger.ExecuteStatement('AppDomain.CurrentDomain.SetData("Stepping", "True")');
dte.Debugger.StepOver();
dte.Debugger.ExecuteStatement('AppDomain.CurrentDomain.SetData("Stepping", null)');

You'll have to install the macro add-in - it's the one from Microsoft, so no worries.

This allows you to use a simple conditional breakpoint:

AppDomain.CurrentDomain.GetData("Stepping") != null

(If you want, it's very easy to make the breakpoint using a macro as well)

Now, instead of using the usual Step Over command, you just have to run the macro, and it will automatically break on every breakpoint with the given condition :)

It should be possible to attach a shortcut (and menu command) to the macro, but for some reason, this didn't work for me. If that's a problem for you, you can always make a VSPackage (it's quite simple nowadays, but you'll have to install the Visual Studio SDK), and use pretty much the same code, except in C# instead of JavaScript :)

Note that this assumes you're using VS2012+ - in earlier versions, you can simply use the built-in macros the same way. This means translating the code to VB, of course, but...

like image 104
Luaan Avatar answered Oct 06 '22 07:10

Luaan