当使用.editorconfig配置格式化并启用自动格式化(或运行dotnet format)时,有没有什么方法可以 suppress 特定代码区域/块的格式化?

例如,我有一个定义了1000个常量的文件,这些常量以整齐的列对齐.对于这样的例外,我想使用如下内容

class Example
{
#preserve-format
   // A block I want automatic formatting to just ignore
   const int A      = 123;
   const int Longer = 234;
   const int ...
   ...
#end-preserve-format

   // Formatter works normally here
   public void Foo() 
   { 
   } 
}

有没有什么可以做到这一点?

推荐答案

可以使用#pragma

class Example
{
#pragma warning disable format
   // A block I want automatic formatting to just ignore
   const int A      = 123;
   const int Longer = 234;
   const int ...
   ...
#pragma warning restore format

   // Formatter works normally here
   public void Foo() 
   { 
   } 
}

Csharp相关问答推荐

Dapper是否可以自动扩展类成员

C#相同名称的枚举方法和normal方法,参数类型不同

使用LINQ to XML获取元素值列表是不起作用的

使用LayoutKind在C#中嵌套 struct .显式

如何在C#中将对象[*,*]直接转换为字符串[*,*]?

实体框架核心中的ComplexType和OwnsOne有什么不同?

我想在文本框悬停时在其底部显示一条线

.NET SDK包中的官方C#编译器在哪里?

在使用Audit.NET的AuditTrail实现中,如何逐月将数据摄取到AzureTableStorage?

为什么C#认为这个非托管 struct 有一个重叠

单元测试:模拟返回空

当索引和外键是不同的数据类型时,如何设置导航属性?

EF核心区分大小写的主键

为什么我可以用硬编码的集合而不是变量来设置没有setter的IList属性的值?

如何允许数组接受多个类型?

在.NET8中如何反序列化为私有字段?

类/值和日期的泛型方法

如何在特定时间间隔运行多个后台任务?

Xamarin Forms应用程序中登录页面的用户名和密码编辑文本之间不需要的空格

无法使用直接URL通过PictureBox.ImageLocation加载图像