如何检测未使用的进口? [英] how to detect unused imports?

查看:72
本文介绍了如何检测未使用的进口?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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;

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

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?

推荐答案

如果您要删除它们而不是仅仅检测它们,Visual Studio内置了此功能:

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用户界面中的删除未使用的用法"选项可删除源代码中未使用的using指令,别名和extern别名.有两种方法可以调用该操作:

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:

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

  • 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.

编辑以包含评论中的信息

Franci已将VS 2010的Powershell扩展加入其中,它增加了执行此任务以及其他任务的更多功能:

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

这篇关于如何检测未使用的进口?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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