开发者

Removing all unused references from a project in Visual Studio projects

开发者 https://www.devze.com 2023-01-05 13:57 出处:网络
I just wondered if it possible within various Visual Studio versions to automati开发者_运维问答cally remove all references from a project that were never been used?

I just wondered if it possible within various Visual Studio versions to automati开发者_运维问答cally remove all references from a project that were never been used?

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


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

Removing all unused references from a project in Visual Studio projects


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.


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.


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


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.


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.

Removing all unused references from a project in Visual Studio projects


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.


For anybody coming here looking for Visual studio 2012:

Download and Install Reference Assistant for Visual Studio 11

Later you can do:

Removing all unused references from a project in Visual Studio projects


In Visual Studio 2013 this extension works: ResolveUR


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.


[Update] This feature is only available for .Net core projects.

This feature will be coming to Visual Studio 2019 very soon and already available with Visual Studio 2019 v16.10 Preview 1.

This option is turned off by default, but you can enable it under menu Tools > Options > Text Editor > C# > Advanced. Select the Remove Unused References command in Solution Explorer (Experimental). Once the option is enabled, the Remove Unused References command will appear in the right-click menu of a project name or dependencies node.

Removing all unused references from a project in Visual Studio projects


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

Used and works for Visual Studio 2010.


In the VS2022 (preview at the moment of writing) this comes out of the box for SDK Style Projects (read: .NET Core and newer).

If it is available you can find it in the project context menu:

Removing all unused references from a project in Visual Studio projects

You get to choose what to do with each finding.

Read more about it here.

Pro-tip: Check if your project compiles and runs correctly after applying this. In my experience it doesn't check whether a dependency is used at runtime, for instance.


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


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消