.NET:属性来强制调试器进入一个属性属性、调试器、NET

2023-09-05 23:16:27 作者:一缕青丝烟

Visual Studio中有一个调试设置所谓的步过物业和运营商(仅托管)。这个设置是很有用的,我把它通常开机。

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.

现在每过一段时间,有一个setter / getter属性的项目,这是相当复杂和调试时,我想能踏进去。有没有一种方法来装饰这个属性与属性,以便调试器忽略提及设置的财产,让我步入了吗?

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?

基本上,它应该做的逆 DebuggerStepThroughAttribute 。

Basically, it should do the inverse of the DebuggerStepThroughAttribute.

或者是有另一种方式来实现这一目标?我要做的是目前加强在它之前设置属性的getter / setter方法​​中设置断点,但是这不是很方便,因为它需要我补充/每次我通过特定的code片段步骤时间删除断点。

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.

修改:意见建议重构。并没有真正虽然回答我的问题,而没有必要在我的情况。所谓参与我的意思不是很多code或一些资源密集型code。就我而言,属性setter触发对象(O(1)复杂度,左右两毫秒)内的计算。然而,这算不是很明显,每过一段时间我想的踏进的属性setter由pressing将步入关键。

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.

推荐答案

那么,要做到这一点,你需要一种方法来知道调试器是否处于运行模式,或模式逐步执行。可悲的是,你没有得到你的应用程序内的信息。

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 :)

有应该有可能附加一个快捷方式(和菜单命令)到宏,但由于某些原因,这并没有为我工作。如果这是对你一个问题,你总是可以做一个VSPackage的(这是很简单的今天,但你必须安装Visual Studio SDK),并使用pretty的大同小异code,除了在C#中,而不是JavaScript的:)

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 :)

请注意,这里假设你正在使用VS2012 + - 在早期版本中,你可以简单地使用内置的宏以同样的方式。这意味着翻译过程中的code到VB,但...

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...