Getting started with Reflector Addins

by Jason Haley 30. October 2007 20:11

If you are just thinking you want to write a Reflector addin, you should first look at Introduction to the .NET Reflector Add-In Model.

Here is a very basic walk through creating a simple addin and steps to get it stopped in the debugger:

1.  Open a new VS 2005 dll project

2.  Give it some name (I used 'TestPackage')

3.  Set a reference to your local Reflector.exe

4.  Paste the following code into the class

using System;
using Reflector;
using Reflector.CodeModel;
 
namespace Reflector.Addin
{
    public class TestPackage : IPackage
    {
 
        private IServiceProvider serviceProvider;
        private ICommandBarManager commandBarManager;
        private ICommandBarSeparator separator;
        private ICommandBarButton button;
 
        public void Load(IServiceProvider serviceProvider)
        {
            this.serviceProvider = serviceProvider;
 
            AddButton();
        }
 
        public void Unload()
        {
            RemoveButton();
        }
 
        private void Button_Click(object sender, EventArgs e)
        {
            ProcessClick();    
        }
 
        private void ProcessClick()
        {
            IAssemblyBrowser assemblyBrowser = GetService<IAssemblyBrowser>();
            IWindowManager windowManager = GetService<IWindowManager>();
            ICommandBarManager commandBarManager = GetService<ICommandBarManager>();
            IConfigurationManager configurationManager = GetService<IConfigurationManager>();
            IAssemblyManager assemblyManager = GetService<IAssemblyManager>();
            ILanguageManager languageManager = GetService<ILanguageManager>();
 
            System.Diagnostics.Debugger.Break();
 
        }
 
        private void AddButton()
        {
            this.commandBarManager = GetService<ICommandBarManager>();
            this.separator = this.commandBarManager.CommandBars["Tools"].Items.AddSeparator();
            this.button = this.commandBarManager.CommandBars["Tools"].Items.AddButton("Stop"
                new EventHandler(this.Button_Click));
        }
 
        private void RemoveButton()
        {
            this.commandBarManager.CommandBars["Tools"].Items.Remove(this.separator);
            this.commandBarManager.CommandBars["Tools"].Items.Remove(this.button);
        }
 
        private T GetService<T>()
        {
            return (T)serviceProvider.GetService(typeof(T));
        }
    }
}

5. Compile and run the Reflector.exe that is in the bin directory

6. Go to the View->Add ins ... dialog

7. Click Add ... button and find your dll you just built and choose it

8.  Click Close

9.  Go to the Tools menu, you should now see a Stop item

 image

10.  Click Stop and wait for the dialog below to show

image

11.  Click Debug, you'll then see the following debugger dialog:

image

12.  Choose the project that you just created and compiled

13.  Once you get the 'no source code available' dialog, click Ok and hit F10

image

You are now ready to explore the objects in the ProcessClick() method ... have fun :) 

BTW:  The quickest way to kill Reflector, if needed, is through task manager

Comments (0) | Post RSSRSS comment feed |

Categories:
Tags:

Comments are closed