重新排序 using 并将它们保留在命名空间之外

Sha*_*tin 6 c# resharper stylecop visual-studio

我们希望Reorder using statements,让他们外面namespace。ReSharpernamespace在重新排序时将它们放在里面。

用于放置 using 指令的 Visual Studio 或 Resharper 功能询问如何放置usingsnamespace.这不是我们想要做的。它的答案建议去ReSharper > Options > Code Editing ? C# ? Code Style ? Add 'using' directive to the deepest scope。尽管已取消选择,但在重新usings排序 时,ReSharper 将使用放在namespace.

在此处输入图片说明

我们怎样才能Reorder using statements把它们排除在外namespace

我们尝试过的其他事情:

我们的 StyleCop.Analyzers 规则集包括以下指令相关规则:

SA1200 Using directives must be placed correctly
SA1208 System using directives must be placed before other using directives
SA1209 Using alias directives must be placed after other using directives
SA1210 Using directives must be ordered alphabetically by namespace
Run Code Online (Sandbox Code Playgroud)

鉴于这些规则以及选项中不“添加usings到最深范围”的选择,我们在构建时收到以下警告:

SA1200 Using directive must appear within a namespace declaration.
Run Code Online (Sandbox Code Playgroud)

我们如何配置 ReSharper 以强制 using 指令必须出现命名空间声明之外

Sha*_*tin 6

stylecop.json使用以下设置添加到项目中:

{  
  "$schema": "https://raw.githubusercontent.com/DotNetAnalyzers/StyleCopAnalyzers/master/StyleCop.Analyzers/StyleCop.Analyzers/Settings/stylecop.schema.json",
  "settings": {
    "orderingRules": {
      "usingDirectivesPlacement": "outsideNamespace"
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

然后,通过编辑文件启用使用,stylecop.jsonProjectName.csproj在项目文件中找到以下项目...

<None Include="stylecop.json" />
Run Code Online (Sandbox Code Playgroud)

...并将定义更改为以下内容。

<AdditionalFiles Include="stylecop.json" />
Run Code Online (Sandbox Code Playgroud)

  • 只是为了澄清,这正在更改 StyleCopAnalyzers 的设置,这意味着 ReSharper 不涉及这里。 (2认同)
  • ReSharper 不了解此文件,因此无法遵守该设置。当您调用“重新排序使用”时,是否来自 &lt;kbd&gt;Alt&lt;/kbd&gt;+&lt;kbd&gt;Enter&lt;/kbd&gt; 菜单,如果是,该项目旁边是否有一个小的 Visual Studio 图标? 我认为发生的事情是您实际上是从 ReSharper 的菜单中调用 Visual Studio 的操作。但这不是 ReSharper 在这里做的工作。 (2认同)