Click here to Skip to main content
15,881,248 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.1K   3.5K   93  
Making Reflector into a Visual Studio.NET Add-In
using System;
using System.Windows.Forms;
using System.Reflection;
using System.Diagnostics;
using Reflector;
using Reflector.Configuration;
using Reflector.Reflection;
using Reflector.Browser;
using Reflector.ComponentModel;
using Reflector.UserInterface;
using System.ComponentModel;

namespace Reflector.VSAddIn
{
	/// <summary>
	/// Summary description for ReflectorSingleton.
	/// </summary>
	public class ReflectorSingleton
	{
		public static void SetApplication(object application)
		{
			__singleton = new ReflectorSingleton(application);
		}
		private static ReflectorSingleton __singleton;

		public static ReflectorSingleton Instance
		{
			get
			{
				if(__singleton == null)
				{
					throw new ApplicationException("SetApplication must be called to init singleton");
				}
				return __singleton;
			}
		}

		private ReflectorSingleton(object application)
		{
			_application = application;
		}
		private object _application;

		public object Application
		{
			get { return _application; }
		}
		
		private Type ApplicationType
		{
			get { return _application.GetType(); }
		}

		public Assembly ReflectorAssembly
		{
			get { return _application.GetType().Assembly; }
		}

		public IServiceProvider ServiceProvider
		{
			get { return (IServiceProvider)Application; }
		}

		public Form ApplicationWindow
		{
			get
			{
				object applicationWindow = ApplicationType.GetField("applicationWindow", BindingFlags.NonPublic | BindingFlags.Instance).GetValue(Application);
				Debug.WriteLine(applicationWindow.GetType(), "applicationWindow");
				return (Form)applicationWindow;
			}
		}

		public Form ToolWindow
		{
			get
			{
				if(_toolWindow == null)
				{
					_toolWindow = new Form();
					ToolWindowControl.TextChanged += new EventHandler(control_TextChanged);
					ToolWindowControl.ControlAdded += new ControlEventHandler(_control_ControlAdded);
					ToolWindowControl.Parent.Controls.Remove(ToolWindowControl);
					ToolWindowControl.Font = ReflectorSingleton.Instance.ApplicationWindow.Font;
				}
				return _toolWindow;
			}
		}
		private Form _toolWindow;

		private void control_TextChanged(object sender, EventArgs e)
		{
			_toolWindow.Text = ToolWindowControl.Text;
		}

		private void _control_ControlAdded(object sender, ControlEventArgs e)
		{
			Control control = e.Control;
			ToolWindowControl.Controls.Remove(control);
			control.Dock = System.Windows.Forms.DockStyle.Fill;
			control.Name = "ToolWindow";
			_toolWindow.Controls.Clear();
			_toolWindow.Controls.Add(control);
			_toolWindow.Visible = true;
			_toolWindow.Activate();
		}

		public Control ToolWindowControl
		{
			get { return (Control)ApplicationWindow.GetType().GetField("toolWindow", BindingFlags.NonPublic | BindingFlags.Instance).GetValue(ApplicationWindow); }
		}

		public IBrowser Browser
		{
			get { return (IBrowser)ServiceProvider.GetService(typeof(IBrowser)); }
		}

		public IAssemblyLoader AssemblyLoader
		{
			get { return ((IAssemblyLoader)ReflectorSingleton.Instance.ServiceProvider.GetService(typeof(IAssemblyLoader)));; }
		}

		public IAssemblyManager AssemblyManager
		{
			get { return (IAssemblyManager)ServiceProvider.GetService(typeof(IAssemblyManager)); }
		}
	}
}

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