简体   繁体   English

从 Visual Studio 项目中的项目中删除所有未使用的引用

[英]Removing all unused references from a project in Visual Studio projects

I just wondered if it possible within various Visual Studio versions to automatically remove all references from a project that were never been used?我只是想知道是否可以在各种 Visual Studio 版本中自动删除从未使用过的项目中的所有引用?

In your answer, please specify which version of VS the solution applies to.在您的回答中,请指定该解决方案适用于哪个版本的 VS。

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...如果您安装了 Resharper(插件),您可以访问一项功能,该功能允许您通过解决方案资源管理器 >(右键单击)参考 > 优化参考...

http://www.jetbrains.com/resharper/webhelp/Refactorings__Remove_Unused_References.html 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)动态加载的程序集 (Assembly.LoadFile)
  • Native code assemblies loaded through interop通过互操作加载的本机代码程序集
  • ActiveX controls (COM interop) ActiveX 控件(COM 互操作)
  • Other creative ways of loading assemblies加载程序集的其他创造性方法

在此处输入图片说明

The Resharper extension will do this for you. Resharper 扩展程序将为您执行此操作。

This extension supports Visual Studio 2005 through 2017.此扩展支持 Visual Studio 2005 到 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.虽然编译器不会包含未使用的程序集,但无关的 using 语句和引用会降低 Visual Studio 和 Intellisense 的速度,因为工具必须考虑更多代码。

You can try the free VS2010 extension: Reference Assistant by Lardite group.您可以尝试免费的 VS2010 扩展:Lardite group 的Reference Assistant It works perfectly for me.它非常适合我。 This tool helps to find unused references and allows you to choose which references should be removed.此工具有助于查找未使用的引用,并允许您选择应删除哪些引用。

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会发生 Missing Reference DLL 错误
  6. Copy required DLL from Debug folder从 Debug 文件夹复制所需的 DLL
  7. Repeat 4-6重复 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.这是VS 2017的时代,穴居人还幸存了下来。

In a Visual Basic project there is support to remove "Unused References" (Project-->References-->Unused References).在 Visual Basic 项目中,支持删除“未使用的引用”(项目--> 引用--> 未使用的引用)。 In C# there isn´t such a function.在 C# 中没有这样的函数。

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.在 C# 项目(不使用其他工具)中执行此操作的唯一方法是删除可能未使用的程序集、编译项目并验证编译期间是否发生任何错误。 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 .如果您想知道哪个项目(程序集)依赖于其他程序集,您可以使用NDepend

For Visual Studio 2013/2015/2017 there is an extension that does exactly what you want: ResolveUR .对于 Visual Studio 2013/2015/2017,有一个完全符合您要求的扩展: ResolveUR What this basically does is:这基本上是:

  • reference is removed in the project在项目中删除了引用
  • project is compiled with msbuild项目是用 msbuild 编译的
  • check for build errors检查构建错误
  • restore removed references if there were build errors.如果存在构建错误,则恢复已删除的引用。

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.在 Visual Studio 2017 和 2015 版本中,您可以使用代码映射功能执行此操作,但此功能仅适用于企业版,而不适用于社区专业版。

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.这会将您的 .dll 显示为空白图表中的单个节点。 Right-click on that node in the Code Map and select "Show Assemblies This References."右键单击Code Map中的该节点并选择“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.这将添加一个名为“Externals”的附加节点,可以将其展开以仅显示实际引用的程序集。

在此处输入图片说明

For anybody coming here looking for Visual studio 2012 :对于来这里寻找Visual Studio 2012 的任何人:

Download and Install Reference Assistant for Visual Studio 11下载并安装Visual Studio 11 的参考助手

Later you can do:稍后您可以执行以下操作:

在此处输入图片说明

在 Visual Studio 2013 中,此扩展有效: ResolveUR

Some people suggested to use an awesome tool - Reference Assistant for Visual Studio .有些人建议使用一个很棒的工具——Visual Studio 的参考助手 The problem is that VS2012 is the latest supported Visual Studio.问题是 VS2012 是最新支持的 Visual Studio。 But there is the way to make it work in VS2013 as well ;)但是也有办法让它在 VS2013 中工作;)

And here is how:这是如何:

1) Download Lardite.RefAssistant.11.0.vsix 1) 下载Lardite.RefAssistant.11.0.vsix

2) Change the extension to zip: Lardite.RefAssistant.11.0.vsix -> Lardite.RefAssistant.11.0.zip 2)将扩展名改为zip: Lardite.RefAssistant.11.0.vsix -> Lardite.RefAssistant.11.0.zip

3) Unzip and open the extension.vsixmanifest file in the text editor 3) 在文本编辑器中解压并打开extension.vsixmanifest文件

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) 4)找到所有出现的InstallationTarget Version="[11.0,12.0)"并将它们替换为InstallationTarget Version="[11.0,12.0]" (注意InstallationTarget Version="[11.0,12.0]"括号)

5) Save the file and zip all files so they are on the root zip level 5) 保存文件并压缩所有文件,使其位于根 zip 级别

6) Change the extension of the new zip to vsix 6)将新zip的扩展名改为vsix

7) Install and enjoy :) 7)安装并享受:)

I've tested it with VS2013, thanks source for the tutorial我已经用 VS2013 测试过了,感谢教程的来源

EDIT Add to support VS 2015 Community Edition编辑添加以支持 VS 2015 社区版

<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.

You can use Reference Assistant extension from the Visual Studio extension gallery.您可以使用 Visual Studio 扩展库中的参考助手扩展

Used and works for Visual Studio 2010.使用并适用于 Visual Studio 2010。

[Update] This feature is only available for .Net core projects. [更新]此功能仅适用于 .Net 核心项目。

This feature will be coming to Visual Studio 2019 very soon and already available with Visual Studio 2019 v16.10 Preview 1 .此功能很快就会出现在 Visual Studio 2019 中,并且已经在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 .此选项默认关闭,但您可以在菜单工具 > 选项 > 文本编辑器 > C# > 高级下启用它。 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.启用该选项后,删除未使用的引用命令将出现在项目名称或依赖项节点的右键单击菜单中。

删除 Visual Studio 2019 v16.10 Preview 1 中未使用的引用

Using DevExpress, I follow these instructions:使用 DevExpress,我遵循以下说明:

  1. In VS, go to DevExpress - Editor - Code Cleanup.在 VS 中,转到 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构建您的应用程序

In the VS2022 (preview at the moment of writing) this comes out of the box for SDK Style Projects (read: .NET Core and newer).VS2022 (撰写本文时的预览版)中,这是针对SDK 样式项目(阅读: .NET Core和更新版本)开箱即用的。

If it is available you can find it in the project context menu:如果可用,您可以在项目上下文菜单中找到它: 在此处输入图片说明

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. Pro-tip:在应用它后检查您的项目是否编译并正确运行。 In my experience it doesn't check whether a dependency is used at runtime, for instance.例如,根据我的经验,它不会检查是否在运行时使用了依赖项。

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)全选 (Ctrl-A)
  2. Toggle outline expansion (Ctrl-M, M).切换轮廓扩展(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.扫描每个文件以查找“0 引用”只需一秒钟。

Scanning an entire project takes only a couple of minutes.扫描整个项目只需几分钟。

To remove a reference in Visual C# In Solution Explorer, open the References node under the project node. 在Visual C#中删除引用在“解决方案资源管理器”中,打开项目节点下的“引用”节点。 Right-click a reference and click Remove. 右键单击引用,然后单击“删除”。

In VB2008, it works this way:在 VB2008 中,它是这样工作的:

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.然后你关闭而不添加任何东西。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM