如何为构造函数自定义Visual Studio的私有字段生成快捷方式? [英] How do I customize Visual Studio's private field generation shortcut for constructors?
问题描述
VS 2017(也许是较旧的版本)为我提供了这个方便的小构造函数快捷方式,用于生成私有只读
字段并进行分配。
VS 2017 (and maybe olders versions) gives me this handy little constructor shortcut to generate a private readonly
field and assign it.
截图:
此最终生成一个名为 userService
的私有成员,然后将其分配给:
This ends up generating a private member called userService
and then assigns it with:
this.userService = userService;
这违背了我使用的代码风格,即用前缀<$命名所有私有成员c $ c> _ 导致分配应如下所示:
This goes against the code style that I use which is to name all private members with a prefix _
resulting in assignment that should look like:
_userService = userService;
如何使VS遵循其代码生成快捷方式遵循此代码样式规则?
How can I make it so that VS obeys this code style rule with its code generation shortcuts?
推荐答案
这也可以直接在Visual Studio中实现。只需转到工具->选项->文字编辑器-> C#->代码样式->命名
。
This can be also achieved directly in Visual Studio. Just go to Tools -> Options -> Text Editor -> C# -> Code Style -> Naming
.
- 首先,您需要通过单击管理命名样式按钮来定义新的命名样式:
- 然后单击+号为私有或内部字段定义新规则,该规则使用您的新命名方式:
-
重新启动Visual Studio
Restart Visual Studio
在此之后,当您应用创建并初始化字段重构时,它将以前导下划线命名。
After that, when you apply the "Create and initialize field" refactoring, it will be named with a leading underscore.
这篇关于如何为构造函数自定义Visual Studio的私有字段生成快捷方式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!