By Rod Paddock
Visual Studio provides one of the most powerful IDEs on the market. One under-exploited aspect of this IDE is the extensibility model. Programming IDEs are not static development tools. Development techniques, tools, and concepts change. Extensibility has been built into Visual Studio from its early inceptions. The great thing about Microsoft’s foresight is that you can augment functionality of Visual Studio yourself. This article will demonstrate how to extend the Visual Studio IDE using Visual Studio, the .NET Framework, and the Visual Studio Add-In and automation models.
The Visual Studio IDE has a number of built in extensibility models.
This article will mash the first three together to create a robust solution creation platform. You will learn how to create a basic add-in, how to extend that add-in using the automation model, and finally how to combine solution templates into the mix to make yourself much more productive. See Claudio Lassala’s article, “ Creating and Distributing Packages with the Visual Studio SDK” in this CoDe Focus issue to learn more about how to extend Visual Studio 2008 using VSPackages.
Developers choose to automate the Visual Studio IDE to increase their productivity. How many times have you found yourself performing the same tasks for your projects? You create a new project, make standard settings changes, you add the same folder structure, you add the same programs, add the same references, etc. As you develop more and more applications you may find yourself spending more time doing redundant project setup and configuration. Add-ins help you cut out redundancy and allow you to focus on core development tasks. You can take this mundane set of steps out of the process by using a combination of add-ins, macros, and templates.
As your first step you want to create the basic shell of your add-in. To create an add-in in Visual Studio, do the following:
At this point you want to look at two specific files: <your-project-name>.AddIn and Connect.VB. The .AddIn file contains XML information that is used to register your add-in with the Visual Studio shell. Listing 1 contains the contents of this XML.
The Connect.VB file contains the guts of your add-in. This code implements two interfaces: IDTExtensibility2 and IDTCommandTarget, which the Visual Studio IDE uses to communicate with your add-in. To add custom code to your add-in you’ll use the Connect.VB. file.
You’ll use one of two methods to modify your add-in code: OnConnection and OnExec. The OnConnection method contains code that integrates your add-in into the Visual Studio menu. The most common thing to change in this method is the icon for your add-in that the Visual Studio menu will display.
The add-in wizard specifies a “smiley face” icon by default. I’ll bet that might not go over too well in a professional environment. To change the icon on the toolbar, look for the line containing the AddNamedCommand2 function.
Dim command As Command _
"Executes the command for CodeFocusAddIn", True,
59, Nothing, _
The 6th parameter (59 in the example) is the ordinal for the icon you want to use. You can find a complete list of the default icons in the book, Working with Microsoft Visual Studio 2005 by Craig Skibo, Marc Young, and Brian Johnson (see Recommended Reading). Changing the ordinal value from 59 to 212 will show a “design mode” icon instead of a “smile face”.
Now you can go to work making this add-in do something. The purpose of this application is to create new solutions. To do this the add-in will contain a Windows Form that will give the developer the opportunity to specify the name and location of the solution. To add these features, perform the following steps:
Dim oForm AsNew frmSolutionProperties
Visual Studio will open another copy of Visual Studio with your add-in populated on the toolbar (Figure 3).
Figure 3: Add-in on the Visual Studio toolbar with designer icon specified
Choose your add-in from the Visual Studio menu to open the frmSolutionProperties form (Figure 4).
Figure 4: New Solution Properties form
Now that you have created the basic structure for your add-in you’ll make it do something useful. You’ll attach your add-in to the Visual Studio IDE thereby giving you the ability to use many functions already built into the Visual Studio shell. You can use the Visual Studio object model to do things like:
In this section you will create a new empty solution using the Visual Studio Object mode. In the first step you want to connect the Visual Studio IDE to your form via a method and a reference variable.
Dim _applicationObject As DTE
Sub AttachDTE(ByVal PassedDTE As DTE)
Me._applicationObject = PassedDTE
oForm.AttachDTE(CType(Me ._applicationObject, DTE))
.SaveAs(Me.txtPath.Text + Me.txtSolutionName.Text
.Open(Me.txtPath.Text + Me.txtSolutionName.Text
Note: When you run your add-in, make sure you fill in valid solution names and an already available path for your Path and Solution name elements. Figure 5 shows a solution created by the new add-in.
Figure 5: New solution created by custom add-in
Creating a solution with no projects doesn’t really make any sense. One of the cool features of Visual Studio is it gives developers the ability to take any open project and create a reusable project template from it. In this next section you’ll create two project templates that you will later incorporate into your add-in.
Figure 6: Custom templates integrated into the Visual Studio shell
Now that you have created a new set of project templates you can incorporate them into your add-in. You need to find the path to your templates and then add them to your project using the AddFromTemplate method.
You use the GetProjectTemplate function of the Visual Studio object model to find the path to your template. The following code demonstrates finding the path to the MiddleTier template.
Dim cMiddleTierPath As String _
This returns the path where Visual Studio stored the cached versions of your templates.
Now you can add a new project to your solution using the AddFromTemplate method, using syntax such as the following:
The parameters to this method are:
You’ll set this last parameter to False as you are adding a project to an existing solution.
Figure 7 shows a newly created solution using the MiddleTier and Windows Front-end templates created in the last section.
Figure 7: Complete solution created by add-in
Now you’ll add the last feature of this project which will manipulate a file found on one of your projects. This example will search all projects for a file named “middletier.vb”. Upon finding this project, the add-in will open that file and insert a small set of code.
The first step to modifying a project file is to find that project file in your solution. Listing 2 performs the following logic:
Figure 8 demonstrates the results of this operation.
Figure 8: New solution created and code changed via add-in
If you want to increase your productivity you should take a good look at creating add-ins to extend the Visual Studio IDE. Add-ins by themselves are not overly useful but when combined with the Visual Studio object model and templates, the real value of add-ins shines.
|Visual Studio 2008 add-ins, combined with the Visual Studio object model and templates, create a useful way to customize the Visual Studio 2008 IDE.|
|Listing 1: .addin file created for a new Visual Studio add-in|
|Listing 2: Searching a solution and all projects for a specific file and replacing the contents with custom code|
If you want to know how to perform a task using the Visual Studio object model look no further than the macro interface in Visual Studio. Visual Studio by default has a number of macros. You can look at these macros for ideas for your own add-ins. Another mechanism you can use to better understand the Visual Studio object model is the Macro Recorder. Turn the Macro Recorder on, perform your task, and let it record the steps for you. Now all you need to do is translate the code into your add-in.
If you want to develop add-ins, this is the book to have. Authors Craig Skibo, Marc Young, and Brian Johnson wrote this must-have for any Visual Studio developer. (Microsoft Press, ISBN-10: 0735623155)