简体   繁体   English

当异步方法不以“Async”结尾时,如何在 Visual Studio 中收到警告?

[英]How do I get a warning in Visual Studio when async methods don't end in 'Async'?

How can I get Visual Studio to give me a naming warning each time I create an asynchronous method that doesn't end in "Async"?每次创建不以“Async”结尾的异步方法时,如何让 Visual Studio 给我一个命名警告?

It's the recommended convention for asynchronous methods, but I often find myself forgetting to add that suffix and a warning would be useful.这是异步方法的推荐约定,但我经常发现自己忘记添加该后缀,警告会很有用。

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 .选择Method ,勾选所有辅助功能选项,然后从Modifiers中选择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添加Async作为后缀并给它一个名字作为AsyncSuffix,然后保存
  • Now press the plus sign and create new naming.现在按加号并创建新的命名。 Select specification as Async Method , select required Style as AsyncPostfix and Severity as Suggestor .选择规范作为Async Method ,选择所需的 Style 作为 AsyncPostfix 和Severity作为Suggestor
  • Click OK and save单击确定并保存

在此处输入图片说明

在此处输入图片说明

在此处输入图片说明

In addition to Visual Studio Text Editor Settings, you can create portable, custom editor settings .editorconfig file.除了 Visual Studio 文本编辑器设置之外,您还可以创建可移植的自定义编辑器设置.editorconfig文件。 Visual Studio 2017 natively supports .editorconfig files. Visual Studio 2017 本身支持.editorconfig文件。

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.通过将.editorconfig文件创建为存储库的一部分并将其推送到存储库,您可以为在该代码库中工作的每个人强制执行一致的编码样式,而不管他们的 Visual Studio 文本编辑器设置如何。

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 文件通过使您能够为每个场景进行配置来解决此问题。

EditorConfig settings take precedence over global Visual Studio text editor settings. EditorConfig 设置优先于全局 Visual Studio 文本编辑器设置。

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右键单击并选择添加新项目或按Ctrl + Shift + A

  3. Choose Text File file from General categories and enter .editorconfig as file name.General类别中选择Text File file 并输入.editorconfig作为文件名。

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 Microsoft.VisualStudio.Threading.Analyzers分析器在规则 VSTHRD200 下提供此功能

More info here https://github.com/microsoft/vs-threading/blob/main/doc/analyzers/VSTHRD200.md更多信息在这里https://github.com/microsoft/vs-threading/blob/main/doc/analyzers/VSTHRD200.md

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在事务内使用Stackexchange.Redis异步方法时的Visual Studio警告 - Visual Studio Warning when using Stackexchange.Redis Async methods inside a Transaction 不等待从 UnhandledException 处理程序调用的异步方法 - Async methods called from UnhandledException handler don't get awaited 异步方法不需要额外的线程? - Async methods don't require additional threads? 异步方法的Moq呼叫基不起作用 - Moq callbase for async methods don't work 为什么某些异步方法具有异步而其他异步方法却没有 - Why some async methods have async while other don't 尝试在另一个线程上运行异步方法时,为什么会收到警告? - Why do I get a warning when trying to run an async method on another thread? 当我不需要响应时,使用async而不等待 - Use async without await when I don't need response 解决警告 CS1998 时如何处理异常:此异步方法缺少“等待”运算符 - How do I handle Exceptions when resolving warning CS1998: This async method lacks 'await' operators 为什么 Visual Studio 会使用所有异步方法自动创建 ApiController? 如何创建带有同步方法的 ApiController? - Why Visual Studio automatically creates ApiController with all async methods? How to create ApiController with sync methods in it? 如何最快地获得第一个异步响应(并且不执行其余的)? - How can I get the first async response fastest (and don't perform the remainder)?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM