Dotnet logo

The .NET Tools Blog

Essential productivity kit for .NET developers


Optimizing Assembly References with ReSharper 6.1

This post is about ReSharper 6.1. For other features in ReSharper 6 (both 6.0 and 6.1), please see previous posts introducing ReSharper 6:

One of the prominent new features in ReSharper 6.1 is the ability to easily manage your existing assembly references, browse usages of referenced assemblies, and remove unused references.

In fact, this is not a single feature but three features. Each of them is outlined below:

  • Optimize References. This is the main feature for analyzing referenced assemblies. It can be invoked on any single project from the project’s own context menu or from the context menu of the References node in Solution Explorer:

    The Optimize References tool window displays assembly references that are both unused and used in the current project, and shows how exactly references are used. The feature does not require your project to be compiled, which means you can invoke it even if your project is currently broken.

    One of the best things about this feature is that it allows you to explore usages of a specific referenced assembly, namespace or type; filter them by kind, group them and navigate from them directly to your source code. The screenshot below illustrates exactly why this project references EasyHttp.dll, which types from that assembly are used and where:

    Different icons mean different kinds of usages like read, write, attribute, or invocation usages — the same usage filtering functionality that is used in all other ReSharper search features based on Find Results window. However, one kind of usages is unique for Optimize References: it’s called “related usages” and indicates implicit (non-direct) assembly reference usages required for the compiler.
    In the simplest case, related usages are caused by a type’s hierarchy. That is, if you use a certain type, you have to reference all assemblies containing that type’s supertypes. Unfortunately, this is just one example along with many others which are much more complicated!
    In addition to exploring assembly usages, you can delete unused assembly references directly from this tool window — the toolbar contains two buttons, Remove all unused references and Remove selected references:

    Note that this feature also deletes all redundant C#, VB.NET or XAML namespace import directives which reference namespaces from the assemblies being removed.
  • Remove Unused References. This refactoring quickly detects unused assembly references without displaying their usages. Since it works faster than Optimize References, it is very useful if you don’t want to a full-fledged usage analysis and you’re just looking for a quick cleanup of your project references.

    Clicking Analyze Used References… button in this dialog box will open the Optimize References tool window for a thorough analysis of reference usage.
  • Safe Delete Assembly Reference. This refactoring either silently removes selected assembly references or opens a confirmation window indicating that some of the selected references are used (and shows how they are used.) The refactoring is available on pressing Alt+Del on an assembly reference (or references) selected in the Solution Explorer, or via Refactor This shortcut menu.

These 3 features introduced in ReSharper 6.1 will definitely help you maintain your solution and get rid of redundant dependencies. By the way, keep in mind that if you want to find out which code is referenced in a given scope other than a project (for example, in a folder or in a method), you can use ReSharper’s Find Symbols External To Scope feature that has been recently made available as the Referenced Code option via Navigate To shortcut menu.

The original post is authored by Kirill Skrygan (@kskrygan on twitter), ReSharper developer and the the author of the Optimize Assembly References feature set.

Comments below can no longer be edited.

8 Responses to Optimizing Assembly References with ReSharper 6.1

  1. Avatar

    Joe White says:

    January 3, 2012

    “It can be invoked on any single project from the project’s own context menu or from the context menu of the References node in Solution Explorer” — So you can’t use this feature for the whole solution at once? Seems like that would be awfully useful (though possibly time-consuming).

    Also: what are the “pin” icons next to some of the assemblies (mscorlib and System.Core in your second screenshot)? And why are some of the usages hidden (“0 of 21 usages shown” in that same screenshot)?

  2. Avatar

    John B says:

    January 4, 2012

    We have a source-controlled folder for 3rd party assemblies but sometimes developers add a reference to a DLL in their own build directory and subsequently check-in this change (because of the infamous “but it works on MY pc…”).

    (I’m also suspecting ReSharper to add references to local build directories, but that’s another story)

    It would be awesome if the “optimize assembly usage” could be given a folder.
    It would then loop all the referenced assemblies, and if it found an identical assembly in the given folder, it would make sure the path was to the assembly in this folder (and not a local bin directory).

  3. Avatar

    Nathan Baulch says:

    January 4, 2012

    I agree with @Joe, please add support for one click solution wide unused reference cleanup.

  4. Avatar

    Jura Gorohovsky says:

    January 8, 2012

    @Joe, @Nathan
    Here’s the feature request to make the functionality available on solution level. Please cast your votes.

    @Joe, a part of usages is shown in this screenshot because “Find related usages” filter is off. As to the “pin” icon, I’m guessing it’s used for assemblies that contain BCL classes (and the Remove action is grayed out for those assemblies) but I can’t tell for sure as the developer of the feature is currently unavailable for comment.

  5. Avatar

    Jura Gorohovsky says:

    January 8, 2012

    @John B That makes sense. I recommend that you file a feature request in ReSharper issue tracker with details, I’ll then assign it to the relevant developer, and it can be discussed further.

  6. Avatar

    James Nail says:

    January 10, 2012

    In the absence of a solution-wide way to optimize assembly references, is it possible to invoke these commands programmatically (so I could just loop through my projects and invoke it)?

  7. Avatar

    Jura Gorohovsky says:

    January 11, 2012

    I assume this is possible by writing a ReSharper plug-in (this needs to be clarified though) but is it really worth the effort?
    It’s really not that great an issue even if you’re about to loop through dozens of projects by hand: invoke “Refactor this” on a project name, select “Remove unused references”, press “Tab” to tab to the “Next” button in the wizard, press “Enter”, and you’re done with deleting redundant references in a project in 10-20 seconds – without even touching a mouse.

  8. Avatar

    Jura Gorohovsky says:

    August 13, 2012

    @Joe, @Nathan, @James “Remove Unused References” works solution-wide since version 7.0

Discover more