Disable Code Formatting for a specific block of code in Visual Studio

拈花ヽ惹草 提交于 2019-12-23 15:50:48

问题


I was wondering if there's a way to disable code formatting for a specific block of code in Visual Studio 2017 (C# 7).

I have this method:

public CarViewModel(ICarsRepo carsRepo)
{
    ...

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

And I would like to format it like so:

public CarViewModel(ICarsRepo carsRepo)
{
    ...

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

But when I press Ctrl K + Ctrl D, it goes back to before.

region inspiration

I would like something to wrap the specific block of code like a region:

public CarViewModel(ICarsRepo carsRepo)
{
    ...

    #region disable_format

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

    #endregion
}

pragma inspiration

Or not neccesarily a region, maybe a pragma used like in this code snippet:

            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.

This is more of an aesthetic preference which might not be shared by most developers, but which I quite like in my code from time to time.


回答1:


This doesn't disable just a block of formatting but it stops Visual Studio from formatting any of your declaration statements.




回答2:


Below matches your need. Not only C#, also for ANY language.

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


来源:https://stackoverflow.com/questions/54849356/disable-code-formatting-for-a-specific-block-of-code-in-visual-studio

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!