VS Addin: Fast Add Reference Dialog - No more Coffee Break!

UPDATE 3: The project is available on Google Code

UPDATE 2: Now solution folders and some other stuff are done, thanks to Huseyin Tufekcilerli

UPDATE: I updated the dlls and the source in the link, now it doesn't crash VS. Hopefully the only problem left is that solutions with multiple solution folders won't be reflected in Projects tab.

I have spent a couple of days on implementing a fast add reference dialog box for Visual Studio (with some help from an existing addin). The time of adding a reference has been a great time to have some coffee, to have lunch, or even a way to save economy (remember, developer time costs money!). If you don’t believe me, take a look at what tweeps say.

It is actually easy once you find some of the extension points of Visual Studio.

In my case, I had to implement IDTExtensibility2 which contains method signatures to be called when a plugin is loaded, unloaded etc.

As first step, I had added “Add Quick Reference” item to the context menu when a project is right clicked. This is the most tricky part as there is almost no documentation on that.

The method that we should implement for this is:

public void OnConnection(object application, ext_ConnectMode connectMode, object addInInst, ref Array custom)

The type of the application parameter implements DTE2 interface and the addInInst parameter implements AddIn interface, which has all we need.

The DTE2 interface has CommandBars property which gives us access to various VS stuff. The matter is to find the right one. After for-looping all CommandBar item, i found that the place I should add my custom item is “Context Menus”->”Project and Solution Context Menus”->”Reference Root”. Man this is hard to locate! Looking forward to MEF in 2010!

applicationObject = (DTE2)application;
addInInstance = (AddIn)addInInst;
CommandBars cb = applicationObject.CommandBars as CommandBars;
CommandBar bar = cb["Context Menus"];
CommandBarPopup cbarControl = bar.Controls["Project and Solution Context Menus"] as CommandBarPopup;
var commandBarControls = cbarControl.Controls;
this.referenceRoots = commandBarControls["Reference Root"] as CommandBarPopup;button = this.referenceRoots.Controls.Add(MsoControlType.msoControlButton,
                           System.Reflection.Missing.Value,
                           System.Reflection.Missing.Value, 1, true)   as CommandBarButton;
button.Caption = "Add Quick Reference";
button.Click += oControl_Click;

There is one thing to be careful about: If you want to handle events of a button, for example, you should hold a reference in your class. Local method variables wouldn’t work. This situation is better told here

Now, I am done. I should now design the dialog itself that looks very similar to the original one. There is another problem: There is no "Windows Explorer Like” control for Windows Forms. There is OpenFileDialog but it is a dialog, not a control. I found the most similar one at GongShell Project which is licensed under GPL.

My current screen looks like the one below:

image

Very similar, even if I say so myself.

One thing to notice here is that the first tab is browse(.NET in original dialog), second is .net (Browse in original dialog) and third one is Projects. I haven’t written “Recent” part as I don’t want to deal with I/O really.

I believe that Browse window is more frequently used than .NET tab. There can be more improvements on that screen, such as having “common” tab which does the copying of commonly used references to a new project but hey this is a demonstration only ! :)

The items in the .NET tab are loaded in the background, and once it is loaded, it will be cached during the lifetime of the application.

I won’t comment more on the code, go grab it and try it. I wont continue developing this little addin, so you are free to do it on your own. Just drop me an email when you do it, though.

I just warn you: Com stuff is like walking on a mine field, and I am not taking any responsibility in case you loose data.

Download the code and the binaries here and put the binaries into Documents\Visual Studio 2008\Addins folder. Have fun!

 

If you like the post and the tool, please Kick! and Shout! it below!

 

kick it on DotNetKicks.com

Shout it


Posted 06-18-2009 11:28 PM by Tuna Toksoz

[Advertisement]

Comments

DotNetKicks.com wrote Fast Add Reference Dialog for VS 2010
on 06-18-2009 4:35 PM

You've been kicked (a good thing) - Trackback from DotNetKicks.com

DotNetShoutout wrote Fast Add Reference Dialog - No more Coffee Break! - Tuna Toksoz - Devlicio.us
on 06-18-2009 4:52 PM

Thank you for submitting this cool story - Trackback from DotNetShoutout

Wyatt Barnett wrote re: VS Addin: Fast Add Reference Dialog - No more Coffee Break!
on 06-18-2009 7:06 PM

I think you might have made my week.  While we are on the subject--why doesn't MS give one the option in Visual Studio to not load the COM tab? It seems to be the slow one and I'd suspect alot of devs never go that way in 2009.

Christopher Bennage wrote re: VS Addin: Fast Add Reference Dialog - No more Coffee Break!
on 06-19-2009 12:23 AM

This is awesome. I had the same idea once, but a friend persuaded me that it was actually more painful to write a VS plugin than to spend hours waiting for a reference to add.

Way to Rock!

Tuna Toksoz wrote re: VS Addin: Fast Add Reference Dialog - No more Coffee Break!
on 06-19-2009 3:31 AM

@Wyatt Barnett

I hope it works well. There are some drawbacks of using this one, such that not all the .NET assembmlies you see in original Add Reference dialog is added, because I couldn't find a way for it. Therefore, I hardcoded the registry path of only .net assemblies v3.0 and v3.5. If you could find a way, please share it

Tuna Toksoz wrote re: VS Addin: Fast Add Reference Dialog - No more Coffee Break!
on 06-19-2009 3:35 AM

@Christopher Bennage

Yes, there is lack of documentation and samples, but with some googling, such simple addin is not hard to implement.

I can't imagine how hard r# and DevExpress guys worked on their framework for the tools, though :)

I hope the addin works for you guys, without any problems.

Jacob Stanley wrote re: VS Addin: Fast Add Reference Dialog - No more Coffee Break!
on 06-19-2009 4:27 AM

I've got VS set up to call the add class template from ReSharper when I hit Ctrl+Shift+A, obviously a bit more limited, but it works a treat :)

Tuna Toksoz wrote re: VS Addin: Fast Add Reference Dialog - No more Coffee Break!
on 06-19-2009 4:32 AM

I've received complaints from one of my friend saying that this crashes VS, can anybody else confirm this? I guess it has something to do with the registry keys.

Socrates wrote re: VS Addin: Fast Add Reference Dialog - No more Coffee Break!
on 06-19-2009 5:51 AM

@Tuna Toksoz: I confirm that! After selecting .net tab, VS2008 Crashes!

Mark Rainey wrote re: VS Addin: Fast Add Reference Dialog - No more Coffee Break!
on 06-19-2009 6:05 AM

Unfortunately switching to the .NET tab crashed my VS2008 as well. Error in the event viewer ... "EventType clr20r3, P1 devenv.exe, P2 9.0.30729.1, P3 488f2b50, P4 addreference, P5 1.0.3456.41749, P6 4a3aad8a, P7 27, P8 30, P9 system.invalidcastexception, P10 NIL."

hack0rz wrote re: VS Addin: Fast Add Reference Dialog - No more Coffee Break!
on 06-19-2009 6:13 AM

why don't you use RAM disk?

solves all your problems...RAM is a cheap nowadays...

Tuna Toksoz wrote re: VS Addin: Fast Add Reference Dialog - No more Coffee Break!
on 06-19-2009 7:07 AM

@Socrates @Mark Rainey

Can you try again, now it should work. (check the beginning of the post)

Tuna Toksoz wrote re: VS Addin: Fast Add Reference Dialog - No more Coffee Break!
on 06-19-2009 7:11 AM

@hack0rz  I wouldn't call it an optimization, though. This dialog can be done faster with no improvement in the hardware.

DotNetBurner - Visual Studio wrote VS Addin: Fast Add Reference Dialog - No more Coffee Break!
on 06-19-2009 7:36 AM

DotNetBurner - burning hot .net content

Rafal Furman wrote re: VS Addin: Fast Add Reference Dialog - No more Coffee Break!
on 06-19-2009 8:09 AM

Great project but I have found bug in it :( In VS2008 when using solution folders, changing view to "Projects" exits addin without any message. Sorry but I don't have time to debug the problem.

Tuna Toksoz wrote re: VS Addin: Fast Add Reference Dialog - No more Coffee Break!
on 06-19-2009 8:12 AM

@Rafal Yes this is a known problem. I don't really understand why microsoft puts Solution Folders into a property called "Projects".

Will check that and post an update when done.

Mark Rainey wrote re: VS Addin: Fast Add Reference Dialog - No more Coffee Break!
on 06-19-2009 8:34 AM

That's sorted it - thanks for the very fast fix.

Tuna Toksoz wrote re: VS Addin: Fast Add Reference Dialog - No more Coffee Break!
on 06-19-2009 8:37 AM

@Mark

Your welcome, don't forget not to use it with solution folders.

Jeff Klawiter wrote re: VS Addin: Fast Add Reference Dialog - No more Coffee Break!
on 06-19-2009 11:35 AM

It's not much easier in 2010 doing what you're doing. Most of the updates were only for doing extensions with the Editor. It's still the same DTE interface for working with things outside the editor.

2010 does make it much easier to get the addin up and running through and the VSIX model will be extremely useful for deploying your extension.

Steven R wrote re: VS Addin: Fast Add Reference Dialog - No more Coffee Break!
on 06-19-2009 12:35 PM

Good to hear my start on the addin helped you out. After posting my addin I received a email from the Lead Program Manager on the VS platform team letting me know that they are working on the performance on the Add Reference dialog.

Here is the email:

"I want to let you know that performance is one of the key focuses for our upcoming Visual Studio 2010 release.  We have done a lot of work around improving overall Add-Reference performance and I hope you will take the time to check out the Beta when it comes out.  What you will see in the Beta is just the beginning of our performance enhancements but it will give you an impression that we are moving in the right direction.  We will also make the dialog sticky so if you are a developer that chooses to browse instead of use the .NET tab you can persist that as your default tab in your first use of Add-Reference."

Thomas G Mayfield wrote re: VS Addin: Fast Add Reference Dialog - No more Coffee Break!
on 06-19-2009 3:24 PM

How I pull projects and not solution folders: csharp.pastebin.com/f5b37a934

Tuna Toksoz wrote re: VS Addin: Fast Add Reference Dialog - No more Coffee Break!
on 06-19-2009 3:29 PM

Thank you, Thomas! Really appreciated. Will put the source code into svn repository and apply your solution!

Thomas G Mayfield wrote re: VS Addin: Fast Add Reference Dialog - No more Coffee Break!
on 06-19-2009 3:32 PM
Rory Becker wrote re: VS Addin: Fast Add Reference Dialog - No more Coffee Break!
on 06-19-2009 5:44 PM

Not so very different from this one.... code.google.com/.../CR_QuickAddReference

Tuna Toksoz wrote re: VS Addin: Fast Add Reference Dialog - No more Coffee Break!
on 06-19-2009 5:48 PM

@Rory

This is something that I haven't seen! definitely better than my little addin! THanks!

Rory Becker wrote re: VS Addin: Fast Add Reference Dialog - No more Coffee Break!
on 06-19-2009 5:56 PM

If you like the whole addin concept, you should really check out the DXCore. It makes the whole plugin thing a real non issue. :)

We have quite a few plugins on the DXCore community  site (http://snurl.com/kgww9) and I am building a page of resources for helping people with creating plugins in this manner (http://snurl.com/fwglx)

Tuna Toksoz wrote re: VS Addin: Fast Add Reference Dialog - No more Coffee Break!
on 06-19-2009 6:05 PM

@Rory

I know it is really hard to deal with native VS extension points, I even faced stuff in such a little addin. Plugin frameworks like R# and DxCore are definitely easier to deal with, I agree. I respect to the guys working on R# and DxCore because of their commitment on such stuff.

Jintan King wrote re: VS Addin: Fast Add Reference Dialog - No more Coffee Break!
on 06-21-2009 7:10 AM
WarNov wrote re: VS Addin: Fast Add Reference Dialog - No more Coffee Break!
on 06-22-2009 7:07 PM

I just copied the binaries to the referenced folder. But When I started  VS2008, the new option didn't appeared. Is there any additional step to make for the adin to work?

Jason N. Gaylord's Blog wrote The Technology Post for June 22nd, 2009
on 06-22-2009 11:29 PM

If you are looking to follow this series, be sure to subscribe to my RSS feed at http://feeds.jasongaylord

ASPInsiders wrote The Technology Post for June 22nd, 2009
on 06-23-2009 12:33 AM

If you are looking to follow this series, be sure to subscribe to my RSS feed at feeds.jasongaylord.com/JasonNGaylord

Tuna Toksoz wrote re: VS Addin: Fast Add Reference Dialog - No more Coffee Break!
on 06-23-2009 4:06 AM

@WarNov

You should copy it into Visual Studio/Addins together with .Addin file. Then you should check Vs->Tools->Addins if you can see it here. If its checkbox is ticked, then it is ok. Go to the project, right click on References there you should see Add Quick Reference thing.

sg_mt wrote re: VS Addin: Fast Add Reference Dialog - No more Coffee Break!
on 06-24-2009 4:07 PM

Unfortunately addin still crashes VS2008.

I debugged it and it stops on file AllAssembliesSource.cs on line 29:

var names = subFolder.GetSubKeyNames();

Reason? key.OpenSubKey(ASSEMBLYFOLDERPATH) in using (var subFolder = key.OpenSubKey(ASSEMBLYFOLDERPATH)) returns null.

Why? Because in my case variable registry "key" (HKEY_CURRENT_USER, called this.AddFoldersFromAssemb(Registry.CurrentUser,folders);

) doesn't contain path specified in ASSEMBLYFOLDERPATH.

So, you need to add line

if (subFolder == null) return;

And then it works just fantastic.

Tuna Toksoz wrote re: VS Addin: Fast Add Reference Dialog - No more Coffee Break!
on 06-24-2009 4:12 PM

The code gets uglier because there is no standard on this registry stuff... how unlucky...

Thank you sg_mt for the feedback!

me wrote re: VS Addin: Fast Add Reference Dialog - No more Coffee Break!
on 07-07-2009 9:18 AM

Downloaded today, crashes VS2008 as soon as I click on the .net tab.

El Bruno wrote [VS2008] AddIn: Add Quick Reference Dialog, un addin que te hará olvidar del dolor de cabeza de la ventana de agregar referencias
on 12-21-2009 4:43 PM

Buenas, seguramente alguna vez te has acordado de toda la familia ascendente del programador de Visual

About The CodeBetter.Com Blog Network
CodeBetter.Com FAQ

Our Mission

Advertisers should contact Brendan

Subscribe
Google Reader or Homepage

del.icio.us CodeBetter.com Latest Items
Add to My Yahoo!
Subscribe with Bloglines
Subscribe in NewsGator Online
Subscribe with myFeedster
Add to My AOL
Furl CodeBetter.com Latest Items
Subscribe in Rojo

Member Projects
DimeCasts.Net - Derik Whittaker

Friends of Devlicio.us
Red-Gate Tools For SQL and .NET

NDepend

SlickEdit
 
SmartInspect .NET Logging
NGEDIT: ViEmu and Codekana
LiteAccounting.Com
DevExpress
Fixx
NHibernate Profiler
Unfuddle
Balsamiq Mockups
Scrumy
JetBrains - ReSharper
Umbraco
NServiceBus
RavenDb
Web Sequence Diagrams
Ducksboard<-- NEW Friend!

 



Site Copyright © 2007 CodeBetter.Com
Content Copyright Individual Bloggers

 

Community Server (Commercial Edition)