当异步方法没有以“ Async”结尾时,如何在 VisualStudio 中获得警告?

如何让 VisualStudio 在每次创建不以“ Async”结尾的异步方法时给我一个命名警告?

这是异步方法的推荐约定,但我经常发现自己忘记添加后缀和警告。

11459 次浏览

From Options,

  • Go to Text EditorBasicCode Style → Naming
  • Select Manage Specifications and Add New Specification
  • Select Method, tick all accessibility options and from Modifiers, select Async.
  • Give the title as Async Method and save
  • Now get into Manage Naming Styles and add New Style. Add Async as suffix and give it a name as AsyncSuffix, and then save
  • Now press the plus sign and create new naming. Select specification as Async Method, select required Style as AsyncPostfix and Severity as Suggestor.
  • Click OK and save

Enter image description here

Enter image description here

Enter image description here

In addition to Visual Studio Text Editor Settings, you can create portable, custom editor settings .editorconfig file. Visual Studio 2017 natively supports .editorconfig files.

By creating the .editorconfig file as part of the repository and pushing it to the repository, you can enforce consistent coding styles for everyone that works in that codebase, regardless of their Visual Studio Text Editor Settings.

The Coding conventions you use on your personal projects may differ from those used on your team's projects. EditorConfig files resolve this problem by enabling you to have a configuration for each scenario.

EditorConfig settings take precedence over global Visual Studio text editor settings.

To do so:

  1. In the Solution Explorer, select the Solution, Project or a folder in the project, depending on the scope which you want to apply the naming rule.

  2. Right click and select Add New Item or Press Ctrl + Shift + A

  3. Choose Text File file from General categories and enter .editorconfig as file name.

Note: The file location can be even in parent folder of your solution. It's not necessary to have it in solution.

Paste the following content in the file:

# Top-most EditorConfig file
root = true


[*.{cs,vb}]


# Async methods should have "Async" suffix
dotnet_naming_rule.async_methods_end_in_async.symbols = any_async_methods
dotnet_naming_rule.async_methods_end_in_async.style = end_in_async
dotnet_naming_rule.async_methods_end_in_async.severity = suggestion


dotnet_naming_symbols.any_async_methods.applicable_kinds = method
dotnet_naming_symbols.any_async_methods.applicable_accessibilities = *
dotnet_naming_symbols.any_async_methods.required_modifiers = async


dotnet_naming_style.end_in_async.required_prefix =
dotnet_naming_style.end_in_async.required_suffix = Async
dotnet_naming_style.end_in_async.capitalization = pascal_case
dotnet_naming_style.end_in_async.word_separator =

More Information:

The Microsoft.VisualStudio.Threading.Analyzers analyzer offers this under the rule VSTHRD200

More info here https://github.com/microsoft/vs-threading/blob/main/doc/analyzers/VSTHRD200.md

tl;dr:

<PackageReference Include="Microsoft.VisualStudio.Threading.Analyzers" Version="17.1.46">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
</PackageReference>