Click here to Skip to main content
13,355,311 members (61,547 online)
Click here to Skip to main content
Add your own
alternative version


95 bookmarked
Posted 15 Aug 2004

Building a Refactoring Plug-in for VS.NET - the Sequel

, 31 Aug 2004
Rate this:
Please Sign up or sign in to vote.
Describes how to extend the original refactor add-in with additional features.

Sample Image


Based on an existing add-in, this article shows a simple framework for creating add-ins that provide multiple refactoring functions. It shows how to interact with the source code model, automating editing functions in the source, and generating new classes.


A year ago, I wrote an article that provided a Visual Studio add-in to refactor a variable into a property. Since then this add-in has been in general use in my work and I have added several other refactoring functions. To this purpose, I implemented a little framework for my add-ins to make it easier to add other refactoring functions.

The add-in now sports the following features:

  • Make Property - point to a variable and turn it into a property with setter and getter methods.
  • Make Function - select a piece of code, turn it into a function, and insert a call to the function at the original location.
  • Make Collection Class - select a class and generate a typed collection class (based on CollectionBase).
  • Make Mock Object - select a class (even framework classes) and generate a collection class that overrides all available functions and properties to allow you to simulate this class.
  • Move To Super Class - take an element and move it to the superclass.
  • What is This - a simple demonstration to show how to pick up the edit point in the code and work out what it is.

The Plug-in Framework

When you create a new project to build a VS plug-in, VS.NET generates a class called connect. This class contains all the necessary interfaces that will be called by VS.NET. There are three kinds of calls:

  1. initialization and cleanup calls: these happen at startup, when add-ins are loaded or when VS.NET shuts down.
  2. the Query call: this call occurs often, whenever VS.NET needs to know whether to enable your command in its popup menus. Your code has to determine the circumstance and then can respond with a status.
  3. the Exec call: your add-in command has been invoked and you need to run it.

You do not need to have one connect class per command, however if you make multiple ones available in this class (as is done here), the connect call has to work out which command is affected in the Query and Exec calls.

The plug-in framework solves this by creating a base class called VS-Plugin. Each kind of command listed above is implemented as a sub class of VS-Plugin. The connect class is now only aware of a collection of these plug-ins, and when a Query or Exec call comes, it simply iterates through its collection and passes the request on to the appropriate class.

Sample Image

The VSPlugIn class also contains several utility functions to do things such as:

  • what is currently highlighted, a class, a function, etc.
  • the ability to link into the necessary command structures into VS.NET.
  • putting up a dialog to ask the user for a target project.
  • displaying messages in the Output window of VS.NET.
  • etc.

Most plug-ins also have a separate class to do the actual work - e.g., the MakeMockObject plug-in class has a class called MockObjectBuilder. This is simple common sense of separating concerns, the plug-in class' purpose is to understand the request's detail and to deliver the resulting code into the right place, the MockObjectBuilder class' purpose is to generate the Mock class.

How to add another Plug-in

There are only a few steps that need to be done:

1. Create a new VSPlugIn subclass

VSPlugIn has several abstract properties and methods which you must implement:

  • cmdName: the short command, e.g.: "MakeProperty".
  • qualifiedName: the fully qualified command, e.g.: "RefactorAddIn.Connect.MakeProperty".
  • IconId: the ID of the icon to show in the menu, e.g.: 54.
  • position: where in the menu to position the command (1= first).
  • shortDescription: the text to show in the menu.
  • long Description: the text for the tool help.
  • doQueryStatus(): implement the logic to determine if the method currently makes sense and should be active or be inactive (or invisible).
  • doExec(): implement the actual action of the command.

2. Extend the Connect class

The connect class has a method called OnStartupComplete. You must add your plug-in to the other plug-in's collection and call the methods to have it show up in the right tool bars.

CommandBar popup = InsertSubMenu("Code Window", "Refactoring");
CommandBar classViewCommandBar = applicationObject.CommandBars["Class View Item"];

VSPlugIn plugin = new PropertyMaker(applicationObject, addInInstance);
plugin.InsertCommand(popup, false);

The first two lines are already given, they provide two commandbars, one a separate popup menu that is part of the Code Window menu (the one that pops up when you right click in the code window), and the other is the menu that pops up when you right click in the Class Explorer.

You need to add code in similar to the last four lines shown above:

  1. create an instance of your plug-in.
  2. add the plug-in into the plug-ins collection.
  3. if you want the plug-in be available for each commandbar, call the plug-in's InsertCommand method. The boolean should only be true for special circumstances when developing - see below in Points of Interest.

The above example makes the property maker available on both menus. You can target other menubars or leave them out altogether. In this instance, you can still bind to your command via the keyboard mapping available in the Tools->Options dialog (select Folder Environment and item Keyboard) - or you can call your command from macros.

Points of Interest

Programming Visual Studio.NET is not for the faint hearted. Good documentation is not easy to come by. However, I found the book "Inside Microsoft Visual Studio. NET" (Brian Johnson, Craig Skibo, Marc Young, Microsoft Press, ISBN 0-7356-1874-7) to be an excellent help. Still you need to read it several times.

Visual Studio .NET is written in COM, which makes life less pleasant. Here are a few pointers:

  1. Collections start with index 1.
  2. If you ask a collection for something it doesn't have, you won't get a null but an exception. So if you want to check if something is in the collection, you have to write something like this:
    protected bool HasCommand(string commName,out Command command)
        command = null;
            Commands commands = _applicationObject.Commands;
            command = commands.Item(commName,-1);
            return true;
        catch (System.Exception){}
        return false;
  3. Not all commands will work under all circumstances. For instance, the command
    , which inserts a new class into the code model, only works with C++, not for C# or VB.NET. However, InsertMethod does.
  4. Not all windows give their content out. While the documentation makes a lot of effort telling us how to access the tree controls in some of their windows, you need to almost read between the lines to work out that this is not available for some windows, e.g.: the Class View window.
  5. It is highly recommended to peruse and understand other examples of plug-ins. Graceful acknowledgement to Erick Sgarbi and his article on a VS Plug-in where I borrowed several techniques including the one above.
  6. If you are interested in CodeDom (defining code constructs and then generating source code), look at my CollectionBuilder class for an example.
  7. When you publish your add-in to Visual Studio, you create an object called a command. This command is the structure by which other parties in VS.NET can recognize your plug-in and invoke it. Be aware that on shutdown, VS.NET saves this command and reloads it on restart. Hence you do not have to recreate a command every time your add-in is loaded. In fact, you shouldn't, because this gums up the works. The VSPlugIn::InsertCommand method has a boolean parameter called deleteIfExists; if true, it will delete existing commands before creating a new one. This parameter should only be true during developing and debugging your add-in. In default mode (=false), it will try to find and use the existing command and create one only if it doesn't exist yet.
  8. It is also possible to create during debugging multiple instances of controls (the things that make a command visible in a pop up menu). The InsertCommand method tries to flush these out by calling flushControls(). If you don't do this, all kinds of weird things can happen, sending you around the bend.


  • Version 1.1 - Based upon feedback I have added several changes
    • Make Property is now configurable in the way the private variable is named.
    • The setup adds a registry entry for HKEY_CURRENTUSER\ Software\ MethodsConsulting\ RefactorAddIn\ PrivatevarCase. Possible values are:
      • underscore - prepends an underscore to the variable
      • m_underscore - prepends an 'm_' to the variable name
      • camel - use camel casing
    • Move To SuperClass now works with Variables, Properties and functions
    • Plus various fixes to smaller annoyances.



This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Stephan Meyn
Web Developer
Australia Australia
I am a Software Engineer/Consultant. My work is focussed on helping teams to get more out of their work. So I teach how to do requirements, analysis and design in a format that is easy to understand and apply.
I help with testing too, from starting developers on automated unit testing to running whole testing teams and how they cooperate with development.

For really big projects I provide complete methodologies that support all of the lifecycle.

For relaxation I paddle a sea kayak around Sydney and the Central Coast or write utilities on rainy days to make my life easier.

You may also be interested in...


Comments and Discussions

GeneralArticle how to write Visual studio plug-in Pin
Ciget5-Nov-09 22:27
memberCiget5-Nov-09 22:27 
QuestionHow could add CommandBar into JS Editor? Pin
vsion.zhao10-Jan-07 0:36
membervsion.zhao10-Jan-07 0:36 
QuestionVS2005 support coming up next? Pin
SaurweinAndreas17-Feb-06 7:27
memberSaurweinAndreas17-Feb-06 7:27 
AnswerRe: VS2005 support coming up next? Pin
jshallard18-Jun-06 16:29
memberjshallard18-Jun-06 16:29 support? Pin
iammudman24-Oct-05 9:35
memberiammudman24-Oct-05 9:35 
GeneralInstallation problem Pin
Tim.Hill13-Feb-05 12:00
memberTim.Hill13-Feb-05 12:00 
GeneralMultiple property generation Pin
solo21-Nov-04 22:59
susssolo21-Nov-04 22:59 
GeneralUML Pin
dshorter12-Nov-04 1:19
memberdshorter12-Nov-04 1:19 
GeneralRe: UML Pin
Stephan Meyn2-Nov-04 1:38
memberStephan Meyn2-Nov-04 1:38 
GeneralExcellent Article! Pin
jaxterama22-Sep-04 20:23
memberjaxterama22-Sep-04 20:23 
GeneralRe: Excellent Article! Pin
Stephan Meyn24-Sep-04 16:13
memberStephan Meyn24-Sep-04 16:13 
GeneralRe: Excellent Article! Pin
computerguru9238224-Jan-06 17:55
membercomputerguru9238224-Jan-06 17:55 
GeneralNaming of variable Pin
cvollmer30-Aug-04 0:40
membercvollmer30-Aug-04 0:40 
GeneralRe: Naming of variable Pin
Stephan Meyn30-Aug-04 4:12
memberStephan Meyn30-Aug-04 4:12 
GeneralRe: Naming of variable Pin
Lakshmipathy10-Aug-06 20:31
memberLakshmipathy10-Aug-06 20:31 
General"Move to super class" Pin
GeneD27-Aug-04 18:25
memberGeneD27-Aug-04 18:25 
GeneralRe: "Move to super class" Pin
Stephan Meyn28-Aug-04 1:33
memberStephan Meyn28-Aug-04 1:33 
GeneralRe: "Move to super class" Pin
GeneD28-Aug-04 7:55
memberGeneD28-Aug-04 7:55 
GeneralRe: "Move to super class" Pin
Stephan Meyn28-Aug-04 14:21
memberStephan Meyn28-Aug-04 14:21 
GeneralRe: "Move to super class" Pin
GeneD28-Aug-04 21:19
memberGeneD28-Aug-04 21:19 
GeneralRe: "Move to super class" Pin
Stephan Meyn28-Aug-04 22:49
memberStephan Meyn28-Aug-04 22:49 
GeneralRe: "Move to super class" Pin
GeneD28-Aug-04 23:45
memberGeneD28-Aug-04 23:45 
GeneralRe: "Move to super class" Pin
Stephan Meyn29-Aug-04 13:20
memberStephan Meyn29-Aug-04 13:20 
QuestionAny Additional Requests? Pin
Stephan Meyn20-Aug-04 12:23
memberStephan Meyn20-Aug-04 12:23 
AnswerRe: Any Additional Requests? Pin
NinjaPenguin24-Aug-04 2:49
memberNinjaPenguin24-Aug-04 2:49 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.180111.1 | Last Updated 1 Sep 2004
Article Copyright 2004 by Stephan Meyn
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid