Ways to use .Net Reflector #2 : Create your own add-ins

by Jason Haley 26. February 2007 20:11

It's been awhile since I wrote Ways to use .Net Reflector #1, now it is time to get back to these entries ... besides I'm organizing my material for my Code Camp session :)

You've probably seen the CodePlex .Net Reflector Add-Ins site (and hopefully used a few), but have you thought about writing your own add-in?  In this entry, I hope to show you a few things in order to help you get started.

So where do you start?

Two words: Problem definition.  If you have used Reflector for awhile, I'm sure you've come up with a few things that you wished you could add to it ... or change about it.  For me, I tend to have a lot of assemblies listed in reflector and sometimes wish the assemblies were in alphabetical order - so I can find things faster :)  Not sure if you've ever wanted this same functionality or not ... but let's build an add-in to order the list anyway.

Getting started

Do some research.  Checkout some of the resources for getting familiar with Reflector Add-ins, such as Introduction to the .Net Reflector Add-in Model or just view some of the existing add-ins in Reflector (that is the whole spirit of this isn't it?).

Now lets see how easy it really is to get this to work ....

By the way, I am assuming you have VS 2005 and have downloaded the latest version of Reflector (can download on Reflector's site) in order for you to actually be able to run this code.

Step 1:

Create a dll project and name it Reflector.AssemblyListSorter or something like that and set a reference to Reflector.exe.

Step 2:

Take some time and look over the interfaces and objects available in the Reflector, Reflector.CodeModel and Reflector.CodeModel.Memory namespaces ... there are alot of them.

Step 3:

Add the using statements we are going to need to deal with the Reflector objects and the collection objects:

using System;

using System.Collections.Generic;

using Reflector;

using Reflector.CodeModel;

Step 4:

Make your class implement IPackage

public class SortPakage : IPackage


    public void Load(IServiceProvider serviceProvider)



    public void Unload()




Step 5:

Determine what objects you need to implement your add-in functionality (this may take some digging).  The best way (besides just looking at the Reflector namespaces) is to look at existing add-ins.

To re-order the assembly list, we will need an IAssemblyManager to access the Assemblies list ... and we will need a way to access the functionality when complete - ie. menu item.  In order to add an item to the Tools menu (seems to work for other add-ins so should work for us) - you'll need the following objects: ICommandBarManager and ICommandBarButton (optionally an ICommandBarSeparator).

Now that we know the objects, we just need to add the memeber fields and grab the types out of the serviceProvider in the Load() Method, like this:

private IAssemblyManager assemblyManager;

private ICommandBarManager commandBarManager;

private ICommandBarSeparator separator;

private ICommandBarButton button;

public void Load(IServiceProvider serviceProvider)


    this.assemblyManager = (IAssemblyManager)serviceProvider.GetService(typeof(IAssemblyManager));

    this.commandBarManager = (ICommandBarManager)serviceProvider.GetService(typeof(ICommandBarManager));

    this.separator = this.commandBarManager.CommandBars["Tools"].Items.AddSeparator();

    this.button = this.commandBarManager.CommandBars["Tools"].Items.AddButton("Order Assembly &List", new EventHandler(this.OrderAssemblyListButton_Click));


Step 6:

Add the clean up code to remove the menu items we added in the unload method:

public void Unload()





Step 7:

Implement the click handler ... which in this case reorders the list in the easiest way I could think of - have the SortedList do it for me :)

private void OrderAssemblyListButton_Click(object sender, EventArgs e)


    IAssemblyCollection assemblies = this.assemblyManager.Assemblies;

    SortedList<string, IAssembly> newAssemblyOrder = new SortedList<string, IAssembly>();

    // Add to new SortedList

    foreach(IAssembly assembly in assemblies)


        newAssemblyOrder.Add(assembly.Name, assembly);


    // Remove each item and add in order

    foreach (KeyValuePair<string, IAssembly> item in newAssemblyOrder)






That is it!  Of course reordering the list isn't all that interesting, but it is a good place to start in order to get familiar with the needed objects to create a more feature full add-in.

One useful tip in debugging the addin:

Use System.Diagnostics.Debugger.Break(); to get the add-in logic to break wherever you need.

Now that you actually read this far ... go write your own add-in and do me a favor - blog about it!

Comments (7) | Post RSSRSS comment feed |



Comments are closed