visual-studio 如何检测未使用的进口?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4147035/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-22 12:44:24  来源:igfitidea点击:

how to detect unused imports?

visual-studiovisual-studio-2010

提问by

In my code im often adding some import statements like e.g.:

在我的代码中,我经常添加一些导入语句,例如:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Windows.Threading;

During development some of these import statements may become unnecessary because i move the code that required these imports. In eclipse, unused imports are marked by the IDE, but in Visual Studio 2010 I couldn't find any hint. How can i detected them?

在开发过程中,其中一些导入语句可能变得不必要,因为我移动了需要这些导入的代码。在 Eclipse 中,未使用的导入由 IDE 标记,但在 Visual Studio 2010 中我找不到任何提示。我怎样才能检测到它们?

采纳答案by wkl

Visual Studio has this feature built in if you want to remove them rather than just detect them: Automatic Code Generation -> Organize usings

如果您想删除它们而不仅仅是检测它们,Visual Studio 内置了此功能:自动代码生成 -> 组织使用

The Remove Unused Usings option in the Visual Studio user interface removes using directives, using aliases, and extern aliases that are not used in the source code. There are two ways to call the operation:

  • Main Menu - On the Edit menu, point to IntelliSense, point to Organize Usings, and then click Remove Unused Usings.

  • Context Menu - Right-click anywhere inside the code editor, point to Organize Usings, and then click Remove Unused Usings.

Visual Studio 用户界面中的 Remove Unused Usings 选项可删除源代码中未使用的 using 指令、using 别名和 extern 别名。调用操作有两种方式:

  • 主菜单 - 在编辑菜单上,指向 IntelliSense,指向组织使用,然后单击删除未使用的使用。

  • 上下文菜单 - 右键单击​​代码编辑器内的任意位置,指向组织使用,然后单击删除未使用的使用。

Edit to include information from comments

编辑以包含评论中的信息

Franci has chimed in with the Powershell extension to VS 2010 which adds in more functionality to do this task as well as others: http://visualstudiogallery.msdn.microsoft.com/en-us/e5f41ad9-4edc-4912-bca3-91147db95b99

Franci 加入了 VS 2010 的 Powershell 扩展,它添加了更多功能来完成此任务以及其他功能:http: //visualstudiogallery.msdn.microsoft.com/en-us/e5f41ad9-4edc-4912-bca3-91147db95b99

回答by Ogglas

Visual Studio 2019 comes with default code cleanup that fixes this. Shortcut is to press CTRL+K and CTRL+E as default.

Visual Studio 2019 附带了修复此问题的默认代码清理。快捷键是默认按 CTRL+K 和 CTRL+E。

https://docs.microsoft.com/en-us/visualstudio/ide/code-styles-and-code-cleanup?view=vs-2019

https://docs.microsoft.com/en-us/visualstudio/ide/code-styles-and-code-cleanup?view=vs-2019

enter image description here

在此处输入图片说明