如何配置Rider/Resharper使#nullable保留在using块之后?
解决Rider重构时
#nullable指令位置被修改的问题 在使用JetBrains Rider 2022.1.2(Resharper同配置适用)执行「重构此代码→移动到文件夹」操作时,若原代码中#nullable enable位于using块之后,重构后该指令会被自动移至using块前,可通过调整代码风格设置解决此问题:
- 打开Rider设置(快捷键
Ctrl+Alt+S) - 依次进入
Editor → Code Style → C# → Code Generation - 在「Nullable directives」区域,将「Place '#nullable' directives」选项改为「After 'using' directives」
- 保存设置后重新执行重构操作,
#nullable enable将保持在using块之后的原位置
原代码:
using System.Collections.Generic; #nullable enable internal record Response { public IList<decimal>? Items { get; init; } }
修复前重构后代码:
#nullable enable using System.Collections.Generic; internal record Response { public IList<decimal>? Items { get; init; } }
内容的提问来源于stack exchange,提问作者Sergey Rubtsov
相关产品推荐
相关产品推荐

