Click here to Skip to main content
15,896,154 members
Articles / Programming Languages / C#

Building the Reflector Add-In

Rate me:
Please Sign up or sign in to vote.
4.93/5 (30 votes)
17 Jul 2003CPOL5 min read 303.8K   3.5K   93  
Making Reflector into a Visual Studio.NET Add-In
using System;
using System.IO;
using System.Diagnostics;
using System.Collections;
using System.Reflection;
using System.Drawing;
using System.Windows.Forms;
using MutantDesign.ManagedAddIns;
using Reflector.Reflection;
using Reflector.ComponentModel;
using Reflector.UserInterface;
using EnvDTE;
using MutantDesign.ManagedAddIns.Utilities;

namespace Reflector.VSAddIn
{
	public class ReflectorVSAddIn : IPackage
	{
		#region IPackage Members
		public void Load(IServiceProvider serviceProvider)
		{
			Debug.Assert(serviceProvider.GetType().FullName == "Reflector.Application", "The add-in service provider wasn't of type Reflector.Application");
			ReflectorSingleton.SetApplication(serviceProvider);

			if(ManagedAddIn.Current != null)
			{
				ICommandManager manager = (ICommandManager)serviceProvider.GetService(typeof(ICommandManager));
				Type commandManagerType = ReflectorSingleton.Instance.ReflectorAssembly.GetType("Reflector.ComponentModel.CommandManager");
				MethodInfo getMenuMethod = commandManagerType.GetMethod("GetMenu", new Type[] {typeof(bool)});
				Type commandButtonType = ReflectorSingleton.Instance.ReflectorAssembly.GetType("Reflector.ComponentModel.CommandManager+CommandButton");
				PropertyInfo commandProperty = commandButtonType.GetProperty("Command", new Type[] {});

				ManagedAddIn managedAddIn = ManagedAddIn.Current;
				Microsoft.Office.Core.CommandBar parent = managedAddIn.FindOrCreateCommandBar("Code Window/Reflector..");

				CommandBarItem[] items = (CommandBarItem[])getMenuMethod.Invoke(manager, new object[] {false});
				foreach(CommandBarButton reflectorButton in items)
				{
					try
					{
						Debug.WriteLine(reflectorButton.Text, "Text");
						ICommand reflectorCommand = (ICommand)commandProperty.GetValue(reflectorButton, new object[] {});
						Debug.WriteLine(reflectorButton.Shortcut, "Shortcut");
						Debug.WriteLine(reflectorCommand, "reflectorCommand");

						string commandName = reflectorCommand.GetType().Name;
						Debug.WriteLine(commandName, "commandName");
						IManagedCommandTarget target = new ReflectorCommandTargetProxy(reflectorButton, reflectorCommand);
						ManagedAddInsConfiguration.Command commandConfig = new ManagedAddInsConfiguration.Command();
						commandConfig.name = commandName;
						commandConfig.buttonText = reflectorButton.Text;
						commandConfig.tooltip = reflectorButton.Text;
						commandConfig.marshal = true;

						ManagedCommands managedCommands = (ManagedCommands)ManagedAddIn.Current.GetService(typeof(ManagedCommands));
						ManagedCommand managedCommand = managedCommands.AddNamedCommand(commandName, reflectorButton.Text, reflectorButton.Text, true, 2, target, commandConfig, null);
						managedCommand.AddControl(parent, 1, reflectorButton.Image);
					}
					catch(Exception e)
					{
						Debug.WriteLine(e);
					}
				}
			}
		}

		public void Unload()
		{
			// TODO:  Add ReflectorMain.Unload implementation
		}

		private class ReflectorCommandTargetProxy : ManagedCommandTarget
		{
			public ReflectorCommandTargetProxy(CommandBarButton button, ICommand command)
			{
				_button = button;
				_command = command;
			}
			private CommandBarButton _button;
			private ICommand _command;

			public override void Exec(object source, ManagedCommandTarget.ExecEventArgs args)
			{
				ReflectorSynchronization.Synchronize(true);
				if(_command.IsEnabled)
				{
					_command.Execute();
				}
			}

			public override void QueryStatus(object source, ManagedCommandTarget.QueryStatusEventArgs args)
			{
				args.StatusOption = vsCommandStatus.vsCommandStatusEnabled | vsCommandStatus.vsCommandStatusSupported;
			}
		}
		#endregion
	}

	#region Help Command
	public class HelpCommand : IManagedCommandTarget
	{
		public void Exec(object source, ManagedCommandTarget.ExecEventArgs args)
		{
			string help = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Reflector.htm");
			ManagedAddIns.DTE.ItemOperations.Navigate(help, vsNavigateOptions.vsNavigateOptionsNewWindow);
		}

		public void QueryStatus(object source, ManagedCommandTarget.QueryStatusEventArgs args)
		{
			args.StatusOption = vsCommandStatus.vsCommandStatusEnabled | vsCommandStatus.vsCommandStatusSupported;
		}
	}
	#endregion

	#region Reflector Command
	public class ReflectorCommand : IManagedCommandTarget
	{
		public void Exec(object source, ManagedCommandTarget.ExecEventArgs args)
		{
			ReflectorSingleton.Instance.ApplicationWindow.Activate();
		}

		public void QueryStatus(object source, MutantDesign.ManagedAddIns.ManagedCommandTarget.QueryStatusEventArgs args)
		{
			args.StatusOption = vsCommandStatus.vsCommandStatusEnabled | vsCommandStatus.vsCommandStatusSupported;
		}
	}
	#endregion

	#region ReflectCode Command
	public class ReflectCodeCommand : IManagedCommandTarget
	{
		public void Exec(object source, ManagedCommandTarget.ExecEventArgs args)
		{
			ReflectorSynchronization.Synchronize(true);
		}

		public void QueryStatus(object source, ManagedCommandTarget.QueryStatusEventArgs args)
		{
			args.StatusOption = vsCommandStatus.vsCommandStatusEnabled | vsCommandStatus.vsCommandStatusSupported;
		}
	}
	#endregion

	#region ReflectProject Command
	public class ReflectProjectCommand : IManagedCommandTarget
	{
		public void Exec(object source, ManagedCommandTarget.ExecEventArgs args)
		{
			foreach(SelectedItem item in ManagedAddIns.DTE.SelectedItems)
			{
				Project project = item.Project;
				if(project != null)
				{
					string path = ProjectUtilities.GetAssemblyPath(project);
					if(path != null)
					{
						IAssemblyLoader loader = ((IAssemblyLoader)ReflectorSingleton.Instance.ServiceProvider.GetService(typeof(IAssemblyLoader)));
						IAssemblyManager manager = ((IAssemblyManager)ReflectorSingleton.Instance.ServiceProvider.GetService(typeof(IAssemblyManager)));
						IAssembly assembly = loader.LoadAssemblyFile(path);
						manager.OpenAssembly(assembly);

						ReflectorSingleton.Instance.ApplicationWindow.Activate();
					}
				}
			}
		}

		public void QueryStatus(object source, ManagedCommandTarget.QueryStatusEventArgs args)
		{
			args.StatusOption = vsCommandStatus.vsCommandStatusEnabled | vsCommandStatus.vsCommandStatusSupported;
		}
	}
	#endregion

	#region DumpAssemblies Command
	public class DumpAssembliesCommand : IManagedCommandTarget
	{
		public void Exec(object source, ManagedCommandTarget.ExecEventArgs args)
		{
			Debug.WriteLine("AssemblyLoader");
			IAssemblyLoader loader = ReflectorSingleton.Instance.AssemblyLoader;
			foreach(Assembly assembly in loader.GetAssemblies())
			{
				Debug.WriteLine(assembly.Location + ", " + assembly.FullName);
			}
			Debug.WriteLine("");

			Debug.WriteLine("AssemblyManager");
			IAssemblyManager manager = ReflectorSingleton.Instance.AssemblyManager;
			foreach(IAssembly assembly in manager.Assemblies)
			{
				try
				{
					Debug.WriteLine(assembly.CodeBase + ", " + assembly.Assembly.CodeBase);
				}
				catch(Exception e)
				{
					Debug.WriteLine(e);
				}
			}
			Debug.WriteLine("");

			Debug.WriteLine("AppDomain");
			foreach(Assembly assembly in AppDomain.CurrentDomain.GetAssemblies())
			{
				Debug.WriteLine(assembly.Location + ", " + assembly.FullName);
			}
			Debug.WriteLine("");
		}

		public void QueryStatus(object source, ManagedCommandTarget.QueryStatusEventArgs args)
		{
			args.StatusOption = vsCommandStatus.vsCommandStatusEnabled | vsCommandStatus.vsCommandStatusSupported;
		}
	}
	#endregion

}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Web Developer
United Kingdom United Kingdom
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions