Removing all unused references from a project in V

2019-01-10 02:47发布

问题:

I just wondered if it possible within various Visual Studio versions to automatically remove all references from a project that were never been used?

In your answer, please specify which version of VS the solution applies to.

回答1:

If you have Resharper (plugin) installed, you can access a feature that allows you to analyze used references via Solution Explorer > (right click) References > Optimize References...

http://www.jetbrains.com/resharper/webhelp/Refactorings__Remove_Unused_References.html

This feature does not correctly handle:

  • Dependency injected assemblies
  • Dynamically loaded assemblies (Assembly.LoadFile)
  • Native code assemblies loaded through interop
  • ActiveX controls (COM interop)
  • Other creative ways of loading assemblies



回答2:

You can try free VS2010 extension: Reference Assistant by Lardite group. It works perfect for me. This tool helps to find unused references and allows to choose what references should be removed.



回答3:

The Resharper extension will do this for you.

This extension supports Visual Studio 2005 through 2017.

While the compiler won't include unused assemblies, extraneous using statements and references slows down Visual Studio and Intellisense, since there's more code the tools have to consider.



回答4:

In a Visual Basic project there is support to remove "Unused References" (Project-->References-->Unused References). In C# there isn´t such a function.

The only way to do it in a C# project (without other tools) is to remove possible unused assemblies, compile the project and verify if any errors occur during compilation. If none errors occur you have removed a unused assembly. (See my post)

If you want to know which project (assembly) depends on other assemblies you can use NDepend.



回答5:

For anybody coming here looking for Visual studio 2012:

Download and Install Reference Assistant for Visual Studio 11

Later you can do:



回答6:

For Visual Studio 2013/2015/2017 there is an extension that does exactly what you want: ResolveUR. What this basically does is:

  • reference is removed in the project
  • project is compiled with msbuild
  • check for build errors
  • restore removed references if there were build errors.


回答7:

In Visual Studio 2013 this extension works: ResolveUR



回答8:

Some people suggested to use an awesome tool - Reference Assistant for Visual Studio. The problem is that VS2012 is the latest supported Visual Studio. But there is the way to make it work in VS2013 as well ;)

And here is how:

1) Download Lardite.RefAssistant.11.0.vsix

2) Change the extension to zip: Lardite.RefAssistant.11.0.vsix -> Lardite.RefAssistant.11.0.zip

3) Unzip and open the extension.vsixmanifest file in the text editor

4) Find all occurences of InstallationTarget Version="[11.0,12.0)" and replace them with InstallationTarget Version="[11.0,12.0]" (note the closing bracket)

5) Save the file and zip all files so they are on the root zip level

6) Change the extension of the new zip to vsix

7) Install and enjoy :)

I've tested it with VS2013, thanks source for the tutorial

EDIT Add to support VS 2015 Community Edition

<InstallationTarget Version="[14.0,15.0]" Id="Microsoft.VisualStudio.Community" />

Meaning of the brackets

[ – minimum version inclusive.

] – maximum version inclusive. 

( – minimum version exclusive. 

) – maximum version exclusive.


回答9:

You can use Reference Assistant extension from the Visual Studio extension gallery.

Used and works for Visual Studio 2010.



回答10:

With Visual Studio versions 2017 and 2015, you can do this with the Code Map feature, but this feature is only available in the Enterprise Edition, not the Community or Professional versions.

Right-click on the project node in the solution explorer and select 'Show on Code Map.' This will display your .dll as a single node in a blank graph. Right-click on that node in the Code Map and select "Show Assemblies This References." This will add an additional node called "Externals" which can be expanded to show only the assemblies that are actually referenced.



回答11:

All you need is stone and bare knuckle then you can do it like a caveman.

  1. Remove unused namespaces (for each class)
  2. Run Debug build
  3. Copy your executable and remaining namespace references to new location
  4. Run the executable
  5. Missing Reference DLL error will occur
  6. Copy required DLL from Debug folder
  7. Repeat 4-6
  8. Gu Gu Ga Ga?
  9. Throw your stone

You can also rely on your build tools to let you know which reference is still required. It's the era of VS 2017, caveman still survived.



回答12:

Using DevExpress, I follow these instructions:

  1. In VS, go to DevExpress - Editor - Code Cleanup. Under Rules, check 'Remove unused namespace references'. Click OK.
  2. Right-click on the solution, and choose 'Code Cleanup'. The cleanup runs for a few minutes, and finishes.
  3. Build your application


回答13:

The following method does not depend on any 'add-on's and is not very painful.

Step through each of your source files and

  1. Select all (Ctrl-A)
  2. Toggle outline expansion (Ctrl-M, M). This will reduce the file to two lines.
  3. Click on the namespace's '+'. This will show each of the file's classes as a single line. Scan each class's reference count, looking for unreferenced classes.
  4. Click on each of the classes' '+'. This will show each of the class functions as a single line. Scan each function's reference count, looking for unreferenced functions.

Scanning each file looking for '0 reference' takes only a second.

Scanning an entire project takes only a couple of minutes.



回答14:

In VB2008, it works this way:

Project>Add References

Then click on the Recent tab where you can see list of references used recently. Locate the one you do not want and delet it. Then you close without adding anything.



回答15:

To remove a reference in Visual C# In Solution Explorer, open the References node under the project node. Right-click a reference and click Remove.