Analyzing and Optimizing Project References
This feature helps manage your existing project/assembly references, browse usages of the referenced projects/assemblies, and remove unused references. You can invoke this feature on any single project and use the Analyze References window to analyze and optimize project references.
Analyzing project references
The Analyze References tool window displays project/assembly references in the current project, and let you explore how exactly the references are used. To explore usages of a specific assembly, namespace or type, you can filter them by kind and group them.
The symbol icons could help you distinguish between different kinds of symbols. Each usage is displayed with the line number and column; different icons mean different kinds of usages like read, write, attribute, or invocation usage. You can double-click any usage to navigate directly to the source code.
To analyze project references
- In the Solution Explorer, select a project that you want to analyze or its References node.
- Do one of the following:
- Right-click the selection and choose Optimize References in the context menu.
- Press Ctrl+Alt+Y.
- Use controls in the Analyze References window to explore project references.
Removing unused references
The easiest way to clean up project references is to click Remove All Unused References on the toolbar. If you want ro retain some unused references or remove some of the used references (and manually resolve the compilation errors that follow), you can make the corresponding selection and click Remove Selected References .
When you remove project and assembly references, ReSharper also deletes all redundant namespace import directives that reference namespaces from the removed assemblies.
All Visual Studio projects must reference
mscorlib and, starting from Visual Studio 2010,
System.Core assemblies. These assemblies are marked with the pin icon and you cannot remove these references.
References that are needed for compilation, independently of whether they are used or not, are displayed in the References required by the compiler group. References in this group are normally have 'related usages', that is, implicit (non-direct) usages. 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.
NuGet packages are displayed with the corresponding icons . When removing NuGet packages, ReSharper will use the NuGet uninstall mechanism.
There are two more ways of cleaning project and assembly references.
- You can invoke the Remove Unused References command on the project or on the References node in the Solution Explorer.
- You can expand the References node in the Solution Explorer, select the references you want to remove and invoke the Safe Delete refactoring, which either silently removes unused references or opens a dialog showing how and where the selected references are used.
Retaining references to implicitly loaded assemblies
Some project types use references implicitly, which means that these references are only used in runtime. These references cannot be safely removed even though there are no code dependencies on them.
If you still want to use ReSharper cleanup features ( Remove All Unused References button in the Analyze References window and Remove Unused References refactoring), you need to mark the implicitly loaded assemblies as such.
To mark implicitly loaded assemblies
- Open the Analyze References window
- In the Unused references section, find the implicitly used references you need to retain.
- Right-click the references and choose one of the following options:
- Mark reference as used at runtime - to exclude the selected reference from cleanup in the current project
- Mark reference as used at runtime for whole solution - to exclude the selected reference from cleanup in all projects in the solution
- ReSharper will save the information about implicitly used references in its layered settings — in a *.DotSettings.user file either for the project or solution.