18

When working with third party systems, especially very configurable systems that dynamically load providers, controllers, components and so on, I sometimes just want to know when a certain object or class is accessed. Normally, I'd place a breakpoint on any potential lines in my source (a nuisance, but it works), but if source is not available:

How can I instruct Visual Studio 2010 Ultimate to break on any and each access to a given class?

Note: as far as my experience goes, this is not generally possible, but I'd like to see it confirmed

Abel
  • 56,041
  • 24
  • 146
  • 247

4 Answers4

14

Not the most elegant, but if you Ctrl+F public then you can spam between F9 [set breakpoint] then F3 [find next] to set a breakpoint on every public entry point into the class.

You might also want to add breakpoints for protected and internal entry points, and any explicit interface implementations (declarations that don't have public)

Carl Walsh
  • 6,100
  • 2
  • 46
  • 50
11

You can click Debug > New Breakpoint > Breakpoint at Function. Ctrl-B brings you there directly. It'll allow you to break at a specific function.

During debugging, you can see in the Breakpoints-window whether the method is found and will be hit (red round icon) or not (white with circle icon, as of disabled breakpoint), just as with normal breakpoints.

Abel
  • 56,041
  • 24
  • 146
  • 247
SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
  • 1
    Thanks, that's handy for a single method, indeed, even if it isn't loaded. But it doesn't help me with every access to a class, nor do I seem to be able to set a breakpoint this way for the `ctor` or `cctor` (but perhaps I need to experiment more). – Abel Aug 25 '10 at 12:47
  • 2
    @Abel: Try `Namespace.ClassName.ClassName`. – SLaks Aug 25 '10 at 16:42
  • I can't get that to work for all types and methods, but I just decided to purchase Reflector Professional (used the free edition for ages) and it allows me to debug and set breakpoints on the reflected code of any loaded or referenced assembly. – Abel Aug 26 '10 at 08:37
  • 1
    Your suggestion at least works for quite some cases, while not a full answer (that would be: it is not possible), it helped nonetheless. Thanks. – Abel Aug 26 '10 at 08:38
  • 1
    @SLaks, while having a try at [this](https://stackoverflow.com/questions/46090686/add-breakpoint-to-c-sharp-classs-every-methods-in-vs2015), I found that specifying just the class name without a function name or wildcards is accepted by the breakpoint editor dialog, but at runtime, the breakpoint appears hollow with an exclamation mark, and does nothing. Then I read your comment properly... and it worked like a charm. **write the class name twice** why does it not surprise me that this is documented nowhere? – Cee McSharpface Sep 07 '17 at 08:02
  • @dlatikay: It can't validate your entry because you might be referring to a DLL that hasn't loaded yet. – SLaks Sep 07 '17 at 15:00
  • You need to write the classname twice to specify the constructor. – SLaks Sep 07 '17 at 15:00
1

At one time (pre VS2008) you could set a breakpoint at every line in a file by select all (ctrl-a) followed by set breakpoint (F9).

To set just one on the entry to every method takes, if I recall correctly, a macro. Check out John Robbins' blog as a possible source: http://www.wintellect.com/CS/blogs/jrobbins/

[EDIT: ctrl-A, F9 doesn't work in 2010 or 2008, so it must be an earlier version I remember this from]

Richard
  • 106,783
  • 21
  • 203
  • 265
  • hitting F9 with multiple lines selected only sets a breakpoint at the current line. But your suggestion for making a macr is good. – Abel Aug 26 '10 at 08:33
  • @Abel: .. and it clears any other breakpoints already in the selected code. This is a change since some earlier version (it doesn't work in 2008, so maybe I'm remembering 2005?). I'll update the answer. – Richard Aug 26 '10 at 08:41
  • Thanks. And I use 2010 and really can't remember it from earlier editions, but then again, there're always features you don't discover before a next edition comes out. – Abel Aug 26 '10 at 08:44
1

I just made a Visual Studio 2010 addin for this. Check this: http://weblogs.asp.net/uruit/archive/2011/08/04/visual-studio-2010-addin-setting-a-class-breakpoint.aspx

Update

This project now lives on Github. Feel free to contribute.

alf
  • 18,372
  • 10
  • 61
  • 92
  • It doesn't work for me. Add-in Manager lists "BreakAll" but there is no new menu item in the debug menu. Ctrl+D,Z (as shown in the screenshot) doesn't work either. – Qwertie Aug 03 '12 at 19:39
  • @Qwertie Check the Github repo for the latest version of the project. There is also a list of known issues there that you can check out. – alf Aug 03 '12 at 20:01