禁用Visual Studio中特定代码块的代码格式 [英] Disable Code Formatting for a specific block of code in Visual Studio

查看:96
本文介绍了禁用Visual Studio中特定代码块的代码格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道在 Visual Studio 2017 (C#7)中是否可以针对特定的代码块禁用代码格式



我有这种方法:

  public CarViewModel(ICarsRepo carsRepo) 
{
...

制造商= ToSelectList< Manufacturer>();
Categories = ToSelectList< Category>();
States = ToSelectList< State>();
}

我想这样格式化:

  public CarViewModel(ICarsRepo carsRepo)
{
...

制造商= ToSelectList< Manufacturer>();
Categories = ToSelectList< Category>();
States = ToSelectList< State>();
}

但是当我按 Ctrl K + Ctrl D ,它可以追溯到以前。



区域灵感



我想包装一些特定的代码块,例如



< pre class = lang-cs prettyprint-override> public CarViewModel(ICarsRepo carsRepo)
{
...

#region disable_format

制造商= ToSelectList< Manufacturer>();
Categories = ToSelectList< Category>();
States = ToSelectList< State>();

#endregion
}



pragma 灵感



或者不一定是地区,也许是 pragma 在此代码段中的用法如下:

  var parameter = 0; 
var sqlCommand = $ {parameter};
#pragma warning disable EF1000 //可能的SQL注入漏洞。
this.Database.ExecuteSqlCommand(sqlCommand);
#pragma warning restore EF1000 //可能的SQL注入漏洞。

这是一种审美上的偏爱,大多数开发人员可能不会分享,但我很喜欢

解决方案

这不仅会禁用一部分格式化,还会阻止Visual Studio格式化您的任何声明语句。



< img src = https://i.stack.imgur.com/2TmcZ.png alt =在此处输入图片描述>


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.

解决方案

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

这篇关于禁用Visual Studio中特定代码块的代码格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆