visual-studio Visual Studio:检测不需要的程序集
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/96279/
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
Visual Studio: Detecting unneeded Assemblies
提问by Michael Stum
On larger and/or long running projects, I tend to reference many assemblies and namespaces, and often I end up removing some functionality later on or moving it into a different project.
在较大的和/或长时间运行的项目中,我倾向于引用许多程序集和命名空间,并且通常最终会在稍后删除某些功能或将其移动到不同的项目中。
I just wonder, is there a way to check every project (heck, every .cs file) in my whole Visual Studio solution and get a list of all referenced Assemblies and Namespaces that are not actually being used and can be safely removed? I know that ReSharper can do it for a single Code File, but I did not see an option to check all files or to check for unneeded Assemblies.
我只是想知道,有没有办法检查我整个 Visual Studio 解决方案中的每个项目(见鬼,每个 .cs 文件)并获取所有引用的程序集和命名空间的列表,这些程序集和命名空间实际上没有被使用并且可以安全地删除?我知道 ReSharper 可以为单个代码文件执行此操作,但我没有看到检查所有文件或检查不需要的程序集的选项。
Using Visual Studio 2005 and 2008 Professional if that matters.
如果重要,请使用 Visual Studio 2005 和 2008 Professional。
Edit: Thanks so far. The Problem with ReSharper or "Remove and Readd if build breaks" is that it's quite tedious on every single file and assembly (My project has about 120 .cs Files in 7 Assemblies, and references a total of 18 Assemblies outside of the solution), so ideally i'm looking for something "one-click". Big Bonus points for some automatic way that can be used in buildscripts to generate a report :)
编辑:到目前为止谢谢。ReSharper 或“如果构建中断则删除并读取”的问题是每个文件和程序集都非常乏味(我的项目在 7 个程序集中有大约 120 个 .cs 文件,并且在解决方案之外引用了总共 18 个程序集),所以理想情况下,我正在寻找“一键式”的东西。可在构建脚本中使用以生成报告的某种自动方式的大奖励积分:)
采纳答案by Chris Canal
回答by Graeme Bradbury
If you have ReSharper then, select the solution, right click and select cleanup code. Resharper will then go through every code file in the solution.
如果您有 ReSharper,则选择解决方案,右键单击并选择清理代码。然后,Resharper 将检查解决方案中的每个代码文件。
As to removing project references, when the compiler runs it won't add the reference if no code uses that dll.
至于删除项目引用,当编译器运行时,如果没有代码使用该 dll,它将不会添加引用。
回答by Craig
If you have ReSharper installed, then from within the Solution Explorer you can right-click on a reference and click Find Dependent Code. If it comes back with a dialog of results then you're using that reference/assembly. If you get the message "Code dependent on module module namenot found." Then you should be OK to remove that reference/assembly because it's not being used.
如果您安装了 ReSharper,则可以在解决方案资源管理器中右键单击引用并单击Find Dependent Code。如果它返回一个结果对话框,那么您正在使用该参考/程序集。如果您收到消息“未找到依赖于模块模块名称的代码”。那么您应该可以删除该引用/程序集,因为它没有被使用。
回答by Benjol
(Cross-posted from here) Given that VisualStudio (or is it msbuild?) detects unused references and doesn't include them in the output file, you can write a script which parses the references out of the csproj, and compares that with the referenced Assemblies detected by reflexion on the project output.
(从这里交叉发布)鉴于 VisualStudio(或者它是 msbuild?)检测到未使用的引用并且不将它们包含在输出文件中,您可以编写一个脚本来解析 csproj 中的引用,并将其与通过对项目输出的反射检测到的引用程序集。
If you're motivated...
如果你有动力...
回答by Gabe Sumner
I found this question while searching in Google and looking for a way to remove unused "Using" statements from my code.
我在 Google 中搜索并寻找一种方法来从我的代码中删除未使用的“Using”语句时发现了这个问题。
Refactor is great, but it isn't available to me. However, as it turns out Visual Studio 2008 does this on its own.
重构很棒,但我无法使用它。但是,事实证明,Visual Studio 2008 是自行完成的。
Here are the steps:
以下是步骤:
- Load the code in Visual Studio.
- Right-click anywhere on the code page.
- Click "Organize Usings" from the menu.
- Click "Remove Unused Usings"
- 在 Visual Studio 中加载代码。
- 右键单击代码页上的任意位置。
- 从菜单中单击“组织使用”。
- 单击“删除未使用的使用”
Done. I realize this doesn't answer the original question (doing this en masse for an entire project) but it does answer my question.
完毕。我意识到这并没有回答最初的问题(为整个项目集体做这个),但它确实回答了我的问题。
回答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 Russell Myers
As was mentioned by previous responses, Resharperworks well for this as well as a variety of other cases such as ways to make code cleaner and generate certain things for you. The best way to really figure out all the advantages of Resharper is to download the trial, print out a Keymap Cheatsheetand stick it next to you where you're developing at.
正如之前的回复所提到的,Resharper非常适用于此以及各种其他情况,例如使代码更清晰并为您生成某些内容的方法。真正了解 Resharper 的所有优势的最佳方法是下载试用版,打印出Keymap Cheatsheet并将其贴在您正在开发的地方。
Long story short, it does solve this problem but it does a heck of a lot more than just that.
长话短说,它确实解决了这个问题,但它的作用远不止于此。
回答by Sam Erwin
It won't get rid of any references in code to your referenced assemblies (I think), but you can go to the properties of each of your projects, got to References, and click "Unused References...". Visual Studio then gives you the option to remove them right there.
它不会删除代码中对引用程序集的任何引用(我认为),但是您可以转到每个项目的属性,转到引用,然后单击“未使用的引用...”。然后,Visual Studio 为您提供了删除它们的选项。
No way to do it at the solution level though.
但是没有办法在解决方案级别做到这一点。
回答by Yes - that Jake.
Any decent code profiler will do this for you. I like DevPartner personally.
任何体面的代码分析器都会为你做这件事。我个人喜欢 DevPartner。
回答by Darren Kopp
Yeah, i don't think there is one. I just delete some i don't think are needed then build :/
是的,我认为没有。我只是删除一些我认为不需要的然后构建:/
BTW. that will keep using errors. You can use the Visual Studio Power commandsto remove and sort usings, so do that first, then delete random assemblies :D
顺便提一句。这将继续使用错误。您可以使用Visual Studio Power 命令删除和排序使用,因此请先执行此操作,然后删除随机程序集:D

