.net 删除未使用的引用(!=“使用”)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/81597/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Remove unused references (!= "using")
提问by Boris Callens
How can I find and delete unused references in my projects?
如何查找和删除项目中未使用的引用?
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.
我知道您可以轻松删除 vs 2008 中的 using 语句,但这不会删除您项目中的实际引用。引用的 dll 仍将复制到您的 bin/setup 包中。
采纳答案by jlo
*Note: see http://www.jetbrains.net/devnet/message/5244658for another version of this answer.
*注意:有关此答案的另一个版本,请参阅http://www.jetbrains.net/devnet/message/5244658。
Reading through the posts, it looks like there is some confusion as to the original question. Let me take a stab at it.
通读这些帖子,似乎对原始问题有些困惑。让我试一试。
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.
原来的帖子实际上是在问一个问题:“如何识别和删除从一个 Visual Studio 项目到其他未使用的项目/程序集的引用?” 发布者希望程序集不再作为构建输出的一部分出现。
In this case, ReSharper can help you identifythem, but you have to removethem yourself.
在这种情况下,ReSharper 可以帮助您识别它们,但您必须自己删除它们。
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
http://www.jetbrains.com/resharper/features/navigation_search.html#Find_ReferencedDependent_Code
You will either get:
您将获得:
A list of the dependencies on that Reference in a browser window, or
A dialog telling you "Code dependent on module XXXXXXX was not found.".
浏览器窗口中对该引用的依赖项列表,或
一个对话框,告诉您“找不到依赖于模块 XXXXXXX 的代码。”。
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.
虽然您必须“手动”执行此操作,即一次一个参考,但它会完成工作。如果有人以某种方式自动化了这个,我很想听听它是如何完成的。
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).
您几乎可以忽略 .Net Framework 中的那些,因为它们通常不会被复制到您的构建输出(通常 - 尽管对于 Silverlight 应用程序不一定如此)。
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".
有些帖子似乎在回答这个问题:“如何从源代码文件中删除不需要解析该文件中的任何引用的 using 子句 (C#)”。
In this case, ReSharper does help in a couple ways:
在这种情况下,ReSharper 确实在以下几个方面提供帮助:
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:
在动态错误检测期间为您标识未使用的 using 子句。它们显示为代码检查警告 - 文件中的代码将显示为灰色(默认),ReSharper 将提供删除它的提示:
http://www.jetbrains.com/resharper/features/code_analysis.html#On-the-fly_Error_Detection
允许您在代码清理过程中自动删除它们:
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.
最后,意识到 ReSharper 会对您的解决方案进行静态代码分析。因此,如果您有对程序集的动态引用——比如通过反射或在运行时动态加载并通过接口访问的程序集——它不会选择它。在您处理项目时,理解代码库和项目依赖项是无可替代的。我确实发现 ReSharper 功能非常有用。
回答by Spongman
you can use the 'Remove Unused References' extension I wrote:
您可以使用我写的“删除未使用的引用”扩展名:
http://visualstudiogallery.msdn.microsoft.com/9811e528-cfa8-4fe7-9dd1-4021978b5097
http://visualstudiogallery.msdn.microsoft.com/9811e528-cfa8-4fe7-9dd1-4021978b5097
回答by jbe
Removing unused references is a feature Visual Studio 2008 already supports. Unfortunately, only for VB .NET projects.
删除未使用的引用是 Visual Studio 2008 已经支持的功能。不幸的是,仅适用于 VB .NET 项目。
I have opened a suggestion on Microsoft Connect to get this feature for C# projects too:
我在 Microsoft Connect 上提出了一个建议,也为 C# 项目获取此功能:
http://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=510326
http://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=510326
If you like this feature as well then you might vote my suggestion.
如果你也喜欢这个功能,那么你可以投票给我的建议。
回答by Leniel Maccaferri
Try this one: Reference Assistant
试试这个:参考助手
Summary
Reference Assistant helps to remove unused references from C#, F#, VB.NET or VC++/CLI projects in the Visual Studio 2010.
概括
参考助手有助于从 Visual Studio 2010 中的 C#、F#、VB.NET 或 VC++/CLI 项目中删除未使用的引用。
回答by Kirill Skrygan
ReSharper 6.1 will include these features:
ReSharper 6.1 将包括以下功能:
-Optimize references: analyze your assembly references and their usages in code, get list of redundant references and remove them.
-优化引用:分析您的程序集引用及其在代码中的用法,获取冗余引用列表并删除它们。
-Remove Unused References: quick refactoring to remove redundant assembly references.
- 删除未使用的引用:快速重构以删除多余的程序集引用。
-Safe delete on assembly references: will delete assembly references if all of them are redundant, otherwise dispalies usages and can remove only redundant assembly references of the selected list.
- 对装配引用的安全删除:如果所有装配引用都是冗余的,则将删除装配引用,否则会显示用法并且只能删除所选列表的冗余装配引用。
回答by mes
I done this without extension in the VS 2010 Ultimate Architecture->Generate Dependency Graph->By Assembly, it shows used assemblies, and manually removed unused references.
我在 VS 2010 Ultimate Architecture->Generate Dependency Graph->By Assembly 中没有扩展就完成了这个,它显示了使用的程序集,并手动删除了未使用的引用。
回答by toddmo
I have a free answer that works in any version of Visual Studio and any Framework version. It doesn't remove the unused references, but it identifies them.
我有一个适用于任何版本的 Visual Studio 和任何框架版本的免费答案。它不会删除未使用的引用,但会标识它们。
You can use Telerik JustDecompileon your project dll. Just open the dll in JustDecompile and go under Referencesto see what is actually used in the compiled dll.
您可以在项目 dll 上使用Telerik JustDecompile。只需在 JustDecompile 中打开 dll 并深入References查看编译后的 dll 中实际使用的内容。
回答by fat
If you know which references are not used you can remove them manually.
In Solution Explorer, right-click the reference in the Referencesnode, and then click Remove.
如果您知道哪些引用未被使用,您可以手动删除它们。
在解决方案资源管理器中,右键单击“引用”节点中的引用,然后单击“删除”。
回答by hdmq
I think that are copied in bin\, because in the project that removed the reference have reference o other project that have the same reference...
我认为是在 bin\ 中复制的,因为在删除引用的项目中引用了其他具有相同引用的项目...


