visual-studio 用于放置 using 指令的 Visual Studio 或 Resharper 功能

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

Visual Studio or Resharper functionality for placement of using directives

visual-studioresharperusing-directives

提问by larsw

I like to put my using directives inside the current namespace, and not outside as VS and Resharper per default puts them.

我喜欢将 using 指令放在当前命名空间内,而不是像 VS 和 Resharper 默认将它们放在外面那样。

Does anyone know of a macro/standard functionality that sorts/removes unused using directives and puts them insidethe current namespace?

有谁知道对未使用的 using 指令进行排序/删除并将它们放在当前命名空间中的宏/标准功能?

回答by marklam

UPDATE - ReSharper 2016.1: This option is now moved to Code Editing → C# → Code Style → Add 'using' directive to the deepest scope

更新 - ReSharper 2016.1:此选项现在移至代码编辑 → C# → 代码样式 → 将“使用”指令添加到最深范围

Have you tried the ReSharper option:

您是否尝试过 ReSharper 选项:

Languages → C# → Formatting Style → Namespace Imports → Add using directive to the deepest scope

语言 → C# → 格式样式 → 命名空间导入 → 将 using 指令添加到最深范围

I'm not sure whether R#'s code cleanup will reorder the existing ones for you though.

我不确定 R# 的代码清理是否会为您重新排序现有的代码。

回答by silkfire

A picture is worth a thousand words – thus in ReSharper 2017and 2018:

一张图片胜过千言万语——因此在ReSharper 20172018 中

enter image description here

在此处输入图片说明

回答by Chris Schiffhauer

As of version 8.2, this has moved. It is now here:

从 8.2 版开始,这已经发生了变化。现在在这里:

Within Visual Studio
Resharper -> Options...

在 Visual Studio
Resharper -> 选项...

Then in the Options dialog
Code Editing -> C# -> Namespace Imports -> Insert using directives when necessary -> Add using directive to the deepest scope

然后在选项对话框
代码编辑 -> C# -> 命名空间导入 -> 必要时插入 using 指令 ​​-> 将 using 指令添加到最深的范围

enter image description here

在此处输入图片说明

回答by trailmax

As of Resharper 9 this has moved into Code Editing=> C#=> Code Style=> Reference Qualification=> Add 'using' directive to deepest scope.

从 Resharper 9 开始,这已经变成了Code Editing=> C#=> Code Style=> Reference Qualification=> Add 'using' directive to deepest scope

Or you can use new settings search facility and search for deepest scope. The exact setting will come up:

或者您可以使用新的设置搜索工具并搜索deepest scope. 将出现确切的设置:

Add 'using' directive to deepest scope

Add 'using' directive to deepest scope

回答by Nikita G.

StyleCop supports ReSharper integration starting from version 4.7.

StyleCop 从 4.7 版开始支持 ReSharper 集成。

From http://stylecop.codeplex.com/:

http://stylecop.codeplex.com/

4.7 is compatible with JetBrains R#5.1( 5.1.3000.12), R#6.0 (6.0.2202.688), R#6.1 (6.1.37.86), R#6.1.1 (6.1.1000.82) and R#7.0 (7.0.54.77)

4.7 is compatible with Visual Studio 2008, Visual Studio 2010 and Visual Studio 11

This release includes the StyleCop ReSharper plugin(compatible with ReSharper 5.1, 6.0, 6.1, 6.1.1 and 7.0)

4.7 兼容 JetBrains R#5.1( 5.1.3000.12)、R#6.0 (6.0.2202.688)、R#6.1 (6.1.37.86)、R#6.1.1 (6.1.1000.82) 和 R#7.54.7.7.7 )

4.7 兼容 Visual Studio 2008、Visual Studio 2010 和 Visual Studio 11

此版本包括 StyleCop ReSharper 插件(与 ReSharper 5.1、6.0、6.1、6.1.1 和 7.0 兼容)

回答by Aaron Daniels

I've edited my default visual studio templatesto put my using directives inside the namespace.

我已经编辑了我的默认 Visual Studio 模板,将我的 using 指令放在命名空间中。

For sorting and removing unused directives, I use Power Commands for Visual Studio 2008.

为了排序和删除未使用的指令,我使用Power Commands for Visual Studio 2008

回答by Bill Mader

Update for Resharper 2020.1: This option can now be found in Options → Code Editing → C# → Syntax Style → Reference Qualification and 'using' Directives

Resharper 2020.1 更新:现在可以在选项 → 代码编辑 → C# → 语法样式 → 引用限定和“使用”指令中找到此选项

Image for reference

图片供参考