gpt4 book ai didi

c# - 在 Visual Studio 中禁用特定代码块的代码格式

转载 作者:行者123 更新时间:2023-11-30 17:27:33 25 4
gpt4 key购买 nike

如何在 Visual Studio 2017 (C# 7) 中禁用代码格式化

我有这个方法:

public CarViewModel(ICarsRepo carsRepo)
{
...

Manufacturers = ToSelectList<Manufacturer>();
Categories = ToSelectList<Category>();
States = ToSelectList<State>();
}

我想像这样格式化它:

public CarViewModel(ICarsRepo carsRepo)
{
...

Manufacturers = ToSelectList<Manufacturer>();
Categories = ToSelectList<Category>();
States = ToSelectList<State>();
}

但是当我按下 Ctrl K + Ctrl D 时,它又回到原来的状态。

#region 灵感

我想要一些东西来包装特定的代码块,比如 #region:

public CarViewModel(ICarsRepo carsRepo)
{
...

#region disable_format

Manufacturers = ToSelectList<Manufacturer>();
Categories = ToSelectList<Category>();
States = ToSelectList<State>();

#endregion
}

#pragma 灵感

或者不一定是region,可能是一个pragma,就像在这个代码片段中使用的:

            var parameter = 0;
var sqlCommand = $"{parameter}";
#pragma warning disable EF1000 // Possible SQL injection vulnerability.
this.Database.ExecuteSqlCommand(sqlCommand);
#pragma warning restore EF1000 // Possible SQL injection vulnerability.

这更像是一种审美偏好,大多数开发人员可能并不认同,但我不时在我的代码中非常喜欢。

最佳答案

  • Visual Studio ( reference )

    • 要禁用格式:#pragma warning disable format
    • 启用格式化:#pragma warning restore format
        switch (number) {
    #pragma warning disable format
    case 1: cardinal = "one"; animal = "monkey"; break;
    case 2: cardinal = "two"; animal = "horse"; break;
    case 3: cardinal = "three"; animal = "pig"; break;
    case 4: cardinal = "four"; animal = "chicken"; break;
    #pragma warning restore format
    }

  • 骑手 ( doc )

    • 禁用格式化://@formatter:off
    • 启用格式化://@formatter:on
        switch (number) {
    // @formatter:off
    case 1: cardinal = "one"; animal = "monkey"; break;
    case 2: cardinal = "two"; animal = "horse"; break;
    case 3: cardinal = "three"; animal = "pig"; break;
    case 4: cardinal = "four"; animal = "chicken"; break;
    // @formatter:on
    }
  • 合并

        switch (number) {
    #pragma warning disable format // @formatter:off
    case 1: cardinal = "one"; animal = "monkey"; break;
    case 2: cardinal = "two"; animal = "horse"; break;
    case 3: cardinal = "three"; animal = "pig"; break;
    case 4: cardinal = "four"; animal = "chicken"; break;
    #pragma warning restore format // @formatter:on
    }

关于c# - 在 Visual Studio 中禁用特定代码块的代码格式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54849356/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com