如何将一些使用语句设置为不冗余,即使它们是多余的?

时间:2015-12-11 17:16:23

标签: c# visual-studio-2015 resharper settings using-statement

创建时,文件通常以一组常用的using语句开头。有时甚至在充实课程后我也不需要一些自动生成的使用语句。但是,如果最终需要删除它们会导致问题,例如因使用System.Linq删除而导致的问题; 有没有办法告诉Visual Studio / Resharper不要抱怨某些使用语句是多余的?

示例:

using System;
using System.Collections.Generic; // Don't need but want anyway without an error
using System.Linq; // Don't need but want anyway without an error
using System.Net;
using System.Text; // Don't need but want anyway without an error
using Acceptance.EndToEnd.Helpers;
using Acceptance.EndToEnd.Locators;

3 个答案:

答案 0 :(得分:6)

ReSharper提供了一种更好的方法:

enter image description here

答案 1 :(得分:1)

你也可以删除它们,但正如已经指出的那样,将它们留下来并没有坏处。

如果删除它们,Visual Studio / Resharper会根据需要将它们添加回来 - 如果使用Resharper,甚至可以添加System.Linq。

如果你真的想要,你可以通过在点击灯泡时关闭此警告来停止Resharper抱怨:

enter image description here

答案 2 :(得分:0)

您可以使用ChrisF的答案,或者您可以添加ReSharper注释以禁用代码中的警告。

使用

// ReSharper disable RedundantUsingDirective

在文件的开头,禁用整个文件中的警告或使用

// ReSharper disable once RedundantUsingDirective
using Namespace.That.I.Dont.Need

禁用单个using语句的警告或使用

// ReSharper disable RedundantUsingDirective
using Namespace.That.I.Dont.Need
using Another.Namespace.That.I.Dont.Need
// ReSharper restore RedundantUsingDirective
using Namespace.That.I.Do.Need

用于多个名称空间。