visual-studio 从 Visual Studio 项目中的项目中删除所有未使用的引用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3157066/
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
Removing all unused references from a project in Visual Studio projects
提问by KroaX
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。
回答by Ernest
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)
- Native code assemblies loaded through interop
- ActiveX controls (COM interop)
- Other creative ways of loading assemblies
- 依赖注入程序集
- 动态加载的程序集 (Assembly.LoadFile)
- 通过互操作加载的本机代码程序集
- ActiveX 控件(COM 互操作)
- 加载程序集的其他创造性方式


回答by Mathieson
The Resharper extensionwill 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 的速度,因为工具必须考虑更多代码。
回答by Wacky
You can try the free VS2010 extension: Reference Assistantby Lardite group. It works perfectly for me. This tool helps to find unused references and allows you to choose which references should be removed.
您可以尝试免费的 VS2010 扩展:Lardite group 的Reference Assistant。它非常适合我。此工具有助于查找未使用的引用,并允许您选择应删除哪些引用。
回答by Jehof
In a Visual Basic project there is support to remove "Unused References" (Project-->References-->Unused References). In C# there isn′t such a function.
在 Visual Basic 项目中,支持删除“未使用的引用”(项目--> 引用--> 未使用的引用)。在 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. If none errors occur you have removed a unused assembly. (See my post)
在 C# 项目(不使用其他工具)中执行此操作的唯一方法是删除可能未使用的程序集,编译项目并验证编译期间是否发生任何错误。如果没有发生错误,则您已删除未使用的程序集。(见我的帖子)
If you want to know which project (assembly) depends on other assemblies you can use NDepend.
如果您想知道哪个项目(程序集)依赖于其他程序集,您可以使用NDepend。
回答by Claudiu Constantin
For Visual Studio 2013/2015/2017 there is an extension that does exactly what you want: ResolveUR. What this basically does is:
对于 Visual Studio 2013/2015/2017,有一个扩展可以满足您的需求:ResolveUR。这基本上是:
- reference is removed in the project
- project is compiled with msbuild
- check for build errors
- restore removed references if there were build errors.
- 项目中的引用被删除
- 项目是用 msbuild 编译的
- 检查构建错误
- 如果存在构建错误,则恢复已删除的引用。
回答by Eark
All you need is stone and bare knuckle then you can do it like a caveman.
您所需要的只是石头和裸露的指关节,然后您就可以像穴居人一样做到这一点。
- Remove unused namespaces (for each class)
- Run Debug build
- Copy your executable and remaining namespace references to new location
- Run the executable
- Missing Reference DLL error will occur
- Copy required DLL from Debug folder
- Repeat 4-6
- Gu Gu Ga Ga?
- Throw your stone
- 删除未使用的命名空间(对于每个类)
- 运行调试构建
- 将您的可执行文件和剩余的命名空间引用复制到新位置
- 运行可执行文件
- 会发生 Missing Reference DLL 错误
- 从 Debug 文件夹复制所需的 DLL
- 重复 4-6
- 咕咕嘎嘎?
- 扔你的石头
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的时代,穴居人还幸存了下来。
回答by Habib
For anybody coming here looking for Visual studio 2012:
对于来这里寻找Visual Studio 2012 的任何人:
Download and Install Reference Assistant for Visual Studio 11
Later you can do:
稍后你可以这样做:


回答by Glenn Slayden
With Visual Studio versions 2017 and 2015, you can do this with the Code Mapfeature, but this feature is only available in the Enterprise Edition, not the Communityor Professionalversions.
在 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. Right-click on that node in the Code Mapand 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.
右键单击解决方案资源管理器中的项目节点,然后选择“在代码图中显示”。这会将您的 .dll 显示为空白图表中的单个节点。右键单击Code Map中的该节点并选择“Show Assemblies This References”。这将添加一个名为“Externals”的附加节点,可以将其展开以仅显示实际引用的程序集。
回答by General Failure
回答by VladL
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 ;)
有些人建议使用一个很棒的工具——Visual Studio 参考助手。问题是 VS2012 是最新支持的 Visual Studio。但是也有办法让它在 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.vsixmanifestfile 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]"(注意右括号)
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 zipto vsix
6)将new的扩展名zip改为vsix
7) Install and enjoy :)
7)安装并享受:)
I've tested it with VS2013, thanks sourcefor the tutorial
我已经用 VS2013 测试过了,感谢教程的来源
EDITAdd 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.

