A nearshore team from Uruguay, South America (GMT-3) Visual Studio 2010 Addin: Setting a class breakpoint - UruIT Blog
Thursday, August 04, 2011 12:39 PM uruit

Visual Studio 2010 Addin: Setting a class breakpoint

Introduction


There comes a time in the life of every .NET developer when you need Visual Studio to do something that
can only be described as a "class breakpoint": a quick command to set a breakpoint on every access to a class.
Unfortunately, after googling this concept, you'll find out that there's no easy way to accomplish this in Visual Studio.
In this article I present an addin that I created, which adds this and another similar command to the debug menu of the
development environment:

ConsoleApplication1 - Microsoft Visual Studio (Administrator)_2011-08-03_20-40-57

When the command is activated, it sets a breakpoint on every function and property of every class in the current document:

ConsoleApplication1 - Microsoft Visual Studio (Administrator)_2011-08-03_20-38-46

 

This addin can be downloaded here: Installer | Source code


Also, note that this addin is language agnostic, meaning that it will work for C#, Visual Basic, and even native C++ applications.

In the rest of the article I'll show the basic steps to create a simple addin for Visual Studio 2010.

 

Creating an addin project

New Project_2011-08-03_20-48-32

Visual Studio makes it easy to create an addin project by providing a template. In the New project dialog,
select Other project types, extensibility, Visual Studio Add-In.
You'll see that a very simple project is created, with the core logic around a class named Connect. This class manages
the lifecycle of the addin through the methods OnConnection, OnDisconnection, etc.
The class field _applicationObject holds a DTE2 object through which we communicate with the environment.

 

Handling events

In this particular case we want to add a command to the Debug menu after a solution is loaded. Therefore, we will need
to wait until a solution is loaded. All the solution events are exposed through the DTE2.Events.SolutionEvents object:

    _solutionEvents = _applicationObject.Events.SolutionEvents;  
    _solutionEvents.Opened += new _dispSolutionEvents_OpenedEventHandler(OnSolutionOpened);
    _solutionEvents.AfterClosing += new _dispSolutionEvents_AfterClosingEventHandler(OnSolutionClosed);

There's a minor caveat here. I'm keeping the reference to the SolutionEvents object in a field of the Connect class.
If I didn't do this, the SolutionEvents object would be deleted by the garbage collector, and the events would never
be raised.

Adding commands

Once that we handle the opening event, we need to add the command to the user interface:

    object[] contextGUIDS = new object[] { };  
    Commands2 commands = (Commands2)_applicationObject.Commands;
    string debugMenuName = "Debug";
    //Place the command on the debug menu.  
    //Find the MenuBar command bar, which is the top-level command bar holding all the main menu items:

    Microsoft.VisualStudio.CommandBars.CommandBar menuBarCommandBar =
        ((Microsoft.VisualStudio.CommandBars.CommandBars)_applicationObject.CommandBars)["MenuBar"];
    //Find the Debug command bar on the MenuBar command bar:   
    CommandBarControl debugControl = menuBarCommandBar.Controls[debugMenuName];
    CommandBarPopup debugPopup = (CommandBarPopup)debugControl;
    _command = 
        commands.AddNamedCommand2
        (
            _addInInstance,
            "CommandName",
            "Text to show in the menu",
            "Description of the command",
            true,
            Type.Missing,
            ref contextGUIDS,
            (int)vsCommandStatus.vsCommandStatusSupported + (int)vsCommandStatus.vsCommandStatusEnabled,
            (int)vsCommandStyle.vsCommandStylePictAndText,
            vsCommandControlType.vsCommandControlTypeButton
        );
      
    _command.AddControl(debugPopup.CommandBar, 1);

This code grabs the Debug menu and adds the command with the specified parameters. This code should be wrapped in a
try-catch block to handle cases when the command already exists in the menu.
We can also add a keyboard shortcut to the command in the following way:

    _command.Bindings = "Text Editor::ctrl+d, z"; 

Here "Text Editor" defines the scope of the shortcut. For more information see http://msdn.microsoft.com/en-us/library/envdte.command.bindings.aspx

 

Browsing the code

Visual Studio automatically parses the current document and exposes a nice interface to browse the code. A code document
contains a tree of code elements. Each code element can be a namespace, a class, a method, etc, and it contains a
collection of child code elements in it. The root code elements can be accessed in this way:

    CodeElements elementsInDocument = this._applicationObject.ActiveDocument.ProjectItem.FileCodeModel.CodeElements 

   
To show the browsing algorithm, here's a recursive method that shows how to get all the classes in the current document:
   

    private static void RecursiveClassSearch(CodeElements elements, List<CodeClass> foundClasses) 
    {
        foreach (CodeElement codeElement in elements)
        {
            if (codeElement is CodeClass)
            {
                foundClasses.Add(codeElement as CodeClass);
            }
            RecursiveClassSearch(codeElement.Children, foundClasses);
        }
    }
   

 

Managing breakpoints

Managing breakpoints is very straighforward. The interface exposed through this._applicationObject.Debugger.Breakpoints
is pretty self descriptive, and it contains functionally to add, remove and browse through breakpoints.

 

Installer

Once you finished you addin, the best way to distribute it is to use a Visual Studio Installer project. An addin consists
of only two files: an *.AddIn xml file and a dll. The easiest way to distribute them is to install them in the same
directory, anywhere on the target machine (might be in ProgramFiles), and to add that directory to the addins directories
of Visual Studio. The latter can be done easily with a registry key: In the registry editor window of your installation
project, add a string key at "HKLM\Software\Microsoft\VisualStudio\10.0\AutomationOptions\LookInFolders" with the name
[TARGETDIR] and a descriptive name in the value. The installer will resolve the [TARGETDIR] placeholder at runtime.

 

Download

This addin can be downloaded here: Installer | Source code

 

CodePlex project

Here's the CodePlex site for this project: http://breakall.codeplex.com

 

Conclusions

In this article I presented a useful addin for Visual Studio and I also showed how to create customs addins. For more information on creating addins you can visit the MSDN: http://msdn.microsoft.com/en-us/vstudio/ff677564. I hope you find the addin useful as I do (maybe I'll publish a second version in the future) and I hope to see your great addins soon!

Thanks for reading!
Alfonso Cora

  
                  
            Filed under: , , 
    

Comments

# re: Visual Studio 2010 Addin: Setting a class breakpoint

Friday, August 05, 2011 3:00 AM by shandy69

This was a really wonderful article. Thank you for your provided information.

# re: Visual Studio 2010 Addin: Setting a class breakpoint

Friday, August 05, 2011 1:48 PM by George

Thanks Alfonso. Very cool!

# VS Addin:

Friday, August 05, 2011 10:36 PM by DotNetKicks.com

You've been kicked (a good thing) - Trackback from DotNetKicks.com

# re: Visual Studio 2010 Addin: Setting a class breakpoint

Monday, August 08, 2011 2:29 PM by JC

Thanks for the add in. Will you be putting this source code anywhere for anyone else would like to enhance it?

# re: Visual Studio 2010 Addin: Setting a class breakpoint

Monday, August 08, 2011 6:30 PM by uruit

Thanks to everyone for your comments. I'm glad you liked it.

@JC Thanks for the suggestion. I just created a CodePlex project: http://breakall.codeplex.com/

# re: Visual Studio 2010 Addin: Setting a class breakpoint

Wednesday, August 10, 2011 2:11 PM by mpaterson

This looks like exactly what I need!  Unfortunately I ran the installer and restarted VS2010 Ultimate but don't see the option in the Debug menu.  Thoughts?

# re: Visual Studio 2010 Addin: Setting a class breakpoint

Wednesday, August 10, 2011 2:50 PM by uruit

@mpaterson Check the Tools -> Add-in manager to see if the addin is installed. The option should appear in the debug menu right below "Disable all breakpoins" or "Delete all breakpoints". Also make sure that you have the english version of VS 2010.

# re: Visual Studio 2010 Addin: Setting a class breakpoint

Thursday, August 11, 2011 10:13 PM by Bruce

Same here, after installing I can see BreakAll inside Tools -> Add-in manager but the option is no where to be seen in debug menu :)

Does it depend on VS2010 version? I'm using Microsoft Visual Studio 2010 Professional

Version 10.0.30319.341 RTMLDR

Microsoft .NET Framework

Version 4.0.30319 RTMLDR

Thanks!

# re: Visual Studio 2010 Addin: Setting a class breakpoint

Friday, August 12, 2011 10:58 AM by uruit

It should work on any english version of VS 2010. The option appears after you open a solution. You can also try with the keyboard shortcut: Ctrl+D, Z.

# re: Visual Studio 2010 Addin: Setting a class breakpoint

Thursday, September 08, 2011 9:22 AM by thexray

Doesn't work for me as well although it does appear in installed add-in list.

Microsoft Visual Studio 2010 Professional (english)

Version 10.0.40219.1 SP1Rel

Microsoft .NET Framework

Version 4.0.30319 SP1Rel

I have custom shortcut assigned to Ctrl+D, so Ctrl+D, Z shortcut doesn't work for me either. I also can't find anything similar to Set Class Breakpoint command in Options->Keyboard shortcut list.

# re: Visual Studio 2010 Addin: Setting a class breakpoint

Tuesday, September 13, 2011 10:37 PM by uruit

Thanks to everyone for the feedback. I've been testing the addin on different environments, although I couldn't reproduce the issue so far. Those who are experiencing the issue in their environment are more than welcome to contribute to the Codeplex project to see if we can get it working. After all, this is from developers to developers :)

# re: Visual Studio 2010 Addin: Setting a class breakpoint

Wednesday, September 28, 2011 3:24 PM by Buy cheap software online

Bcce42 Not bad post, but a lot of extra !!...

# re: Visual Studio 2010 Addin: Setting a class breakpoint

Thursday, December 15, 2011 3:29 AM by Yerac

Hi, it is a great thing but i have one suggestion. For example i have a big file with about 5000 lines and more than one class in it. It will be nice if breakpoints could be set to all the methods in the file, not just to one that belong to this one class which name is the same as file name.

# re: Visual Studio 2010 Addin: Setting a class breakpoint

Tuesday, February 28, 2012 1:28 PM by Jun

What I find is that, you need to start Visual Studio up with no solution, then load in the solution to have this option enlisted under debug.

Hope this helps some people.

Leave a Comment

(required) 
(required) 
(optional)
(required)