删除未使用的引用(!=“使用") [英] Remove unused references (!= "using")

查看:19
本文介绍了删除未使用的引用(!=“使用")的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何查找和删除项目中未使用的引用?

How can I find and delete unused references in my projects?

我知道您可以轻松删除 vs 2008 中的 using 语句,但这不会删除您项目中的实际引用.引用的 dll 仍将复制到您的 bin/setup 包中.

I know you can easily remove the using statements in vs 2008, but this doesn't remove the actual reference in your projects. The referenced dll will still be copied in your bin/setup package.

推荐答案

*注意:参见 http://www.jetbrains.net/devnet/message/5244658 这个答案的另一个版本.

*Note: see http://www.jetbrains.net/devnet/message/5244658 for another version of this answer.

通读帖子,似乎对原始问题有些困惑.让我试一试.

Reading through the posts, it looks like there is some confusion as to the original question. Let me take a stab at it.

原帖实际上是在问一个问题:如何识别和删除从一个 Visual Studio 项目到其他未使用的项目/程序集的引用?"发布者希望程序集不再作为构建输出的一部分出现.

The original post is really asking the question: "How do I identify and remove references from one Visual Studio project to other projects/assemblies that are not in use?" The poster wants the assemblies to no longer appear as part of the build output.

在这种情况下,ReSharper 可以帮助您识别它们,但您必须自己删除它们.

In this case, ReSharper can help you identify them, but you have to remove them yourself.

为此,在解决方案浏览器中打开引用,右键单击每个引用的程序集,然后选择查找相关代码".见:

To do this, open up the References inth Solution Browser, right mouse click on each referenced assembly, and pick "Find Dependent Code". See:

http://www.jetbrains.com/resharper/features/navigation_search.html#Find_ReferencedDependent_Code

您将获得:

  1. 浏览器窗口中对该引用的依赖项列表,或

  1. A list of the dependencies on that Reference in a browser window, or

一个对话框,告诉您未找到依赖模块 XXXXXXX 的代码.".

A dialog telling you "Code dependent on module XXXXXXX was not found.".

如果你得到第二个结果,你可以用鼠标右键点击引用,选择移除,然后从你的项目中移除它.

If you get the the second result, you can then right mouse click the Reference, select Remove, and remove it from your project.

虽然您必须手动"执行此操作,即一次一个参考,但它会完成工作.如果有人以某种方式自动化了这一点,我很想听听它是如何完成的.

While you have to to this "manually", i.e. one reference at a time, it will get the job done. If anyone has automated this in some manner I am interested in hearing how it was done.

您几乎可以忽略 .Net Framework 中的那些,因为它们通常不会被复制到您的构建输出中(通常 - 尽管对于 Silverlight 应用程序不一定如此).

You can pretty much ignore the ones in the .Net Framework as they don't normally get copied to your build output (typically - although not necessarily true for Silverlight apps).

有些帖子似乎在回答这个问题:如何从源代码文件中删除不需要解析该文件中的任何引用的 using 子句 (C#)".

Some posts seem to be answering the question: "How do I remove using clauses (C#) from a source code file that are not needed to resolve any references within that file".

在这种情况下,ReSharper 确实在以下几个方面提供了帮助:

In this case, ReSharper does help in a couple ways:

  1. 在动态错误检测期间为您标识未使用的 using 子句.它们显示为代码检查警告 - 文件中的代码将显示为灰色(默认),ReSharper 将提供删除它的提示:

  1. Identifies unused using clauses for you during on the fly error detection. They appear as Code Inspection Warnings - the code will appear greyed out (be default) in the file and ReSharper will provide a Hint to remove it:

http://www.jetbrains.com/resharper/features/code_analysis.html#On-the-fly_Error_Detection

允许您在代码清理过程中自动删除它们:

Allows you to automatically remove them as part of the Code Cleanup Process:

http://www.jetbrains.com/resharper/features/code_formatting.html#Optimizing_Namespace_Import_Directives

最后,意识到 ReSharper 会对您的解决方案进行静态代码分析.因此,如果您有对程序集的动态引用 - 例如通过反射或在运行时动态加载并通过接口访问的程序集 - 它不会获取它. 理解您的代码是无可替代的处理项目时的基础和项目依赖项.我确实发现 ReSharper 功能非常有用.

Finally, realize that ReSharper does static code analysis on your solution. So, if you have a dynamic reference to the assembly - say through reflection or an assembly that is dynamically loaded at runtime and accessed through an interface - it won't pick it up. There is no substitute for understanding your code base and the project dependencies as you work on your project. I do find the ReSharper features very useful.

这篇关于删除未使用的引用(!=“使用")的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆