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.

Discover more