本文介绍了禁用Visual Studio中特定代码块的代码格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何为 Visual Studio 2017 (C#7)中的特定代码块禁用代码格式?
How can I disable code formatting for a specific block of code in 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>();
}
但是当我按下 + 时,它又回到了原来的状态.
But when I press + , it goes back to what it was.
我想包装一些特定的代码块,例如 #region
:
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
灵感
或者不一定是 region
,也许是像以下代码片段中那样使用的 pragma
:
#pragma
inspiration
Or not necessarily 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.
推荐答案
-
Visual Studio (参考)
- 要禁用格式设置:
#pragma警告禁用格式
- 要启用格式设置:
#pragma警告恢复格式
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 }
- 要禁用格式设置:
-
骑士(文档)
- 要禁用格式设置:
//@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 }
这篇关于禁用Visual Studio中特定代码块的代码格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!