Dig Deeply Into Add-Ins
Build powerful add-ins that leverage the VS.NET IDE's extensibility object model.
by Brian Noyes
January 2003 Issue
Technology Toolbox: VB.NET, C#, COM
In a previous article, I introduced you to the ways you can extend and automate the VS.NET Integrated Development Environment's (IDE's) capabilities with add-ins and macros. That article took the 30,000-foot view, explaining what extensibility options exist and how to create some simple macros and add-ins. Now I'll dive down for a low-level fly-by of some more-detailed issues you should understand to create powerful add-ins that fully exploit the extensibility object model the IDE exposes: the add-in lifecycle, debugging, presenting a UI, the object model, and event handling. In the end, you should be better armed to create real-world add-ins.
Along the way, I'll discuss and walk through developing a sample add-in you can download—InterfaceImplementer—that addresses a frequent, often tedious, coding task that begs for automation: implementing interfaces (see Figure 1). When you implement an interface on a class, you must do a lot of busy work to get all the code in place to derive the class from the interface and implement all the methods and properties. Busy work is exactly what add-ins are good at.
All .NET languages aren't created equal when it comes to creating IDE tools and add-ins. Some classes and methods in the extensibility model work only for either C# or VB—not both. If you're writing code that manipulates source code directly in the editor, you must write separate code sets for each language you want to support. As a result, the functionality of the sample code I'll discuss is limited to implementing interface methods in a C# class—because the CodeClass methods for adding interfaces and methods work only for C#. Doing the same thing for a VB class would require writing a bunch of text-editing code to insert the code elements. Comments in the CodeImplementer class indicate how to approach this task, but the example doesn't implement it fully. (The download code is available in both VB.NET and C#.)
My previous article covered briefly the IDTExtensibility2 interface methods, as well as the connectMode enumeration that's passed into the OnConnection() method when it's called as your add-in loads. You need to understand fully when each method of that interface will be called, how those calls relate to the add-in configuration in the Add-In Manager available in the IDE Tools menu, and the IDE's lifecycle. The download code includes a simple add-in project called LifecycleTester that lets you explore the add-in lifecycle and the interaction among the add-in, its settings in the Add-In Manager, and the state of the IDE. LifecycleTester presents a message box each time the IDE calls a method and tells you which method is called. You should experiment with this add-in, changing the checkboxes in the Add-In Manager for the add-in and seeing which methods are called when. Play with the Startup checkbox and open and close the IDE several times with that box in different states to see how doing so affects when the methods are called.
Back to top
|