You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

JetBrains Rider对象初始化器闭合大括号的EditorConfig配置问题

在JetBrains Rider中保持带尾随逗号的对象初始化器格式的EditorConfig设置

你遇到的问题是:在Rider中编写带尾随逗号的匿名对象初始化器时,格式化后闭合大括号会被移到最后一行元素的同一行,而你希望保持大括号单独一行,同时保留尾随逗号并消除相关警告。

问题代码示例

期望保留的格式:

var p = new
{
    companyId,
    includeHistory,
    getDescriptions,
    includeFlexiFields,
    includeRelatedValues,
};

格式化后异常的格式:

var p = new
{
    companyId,
    includeHistory,
    getDescriptions,
    includeFlexiFields,
    includeRelatedValues, };

移除尾随逗号后的格式(无格式问题但有警告):

var p = new
{
    companyId,
    includeHistory,
    getDescriptions,
    includeFlexiFields,
    includeRelatedValues
};

对应的EditorConfig设置

在项目的.editorconfig文件中添加以下两项配置即可解决问题:

  1. 强制多行结构中始终添加尾随逗号,消除一致性警告:
dotnet_style_trailing_comma_in_multiline = always_for_multiline:suggestion
  1. 强制对象初始化器的闭合大括号单独占一行:
csharp_new_line_before_closing_brace_in_object_initializers = true

添加完成后,重新格式化代码,就能得到你期望的格式,同时不会触发尾随逗号相关警告。

内容的提问来源于stack exchange,提问作者Lasse V. Karlsen

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.15 00:00:57