简体   繁体   English

Visual Studio的高级模板/代码段

[英]Advanced Templates/Snippets for Visual Studio

I'm using Visual Studio 2013 with Resharper 8.1 installed and I'm all about finding shortcuts for regular tasks. 我正在使用已安装Resharper 8.1的Visual Studio 2013,而我全都在寻找常规任务的快捷方式。 One such task that I do regularly is add new constructor dependencies to MVC Controllers. 我经常执行的一项任务是向MVC控制器添加新的构造函数依赖项。 So for example, I might have the following: 因此,例如,我可能具有以下内容:

public class MyController : Controller {
    private readonly ICustomerManager _customerManager;

    public MyController(ICustomerManager customerManager) {
        _customerManager = customerManager;
    }

Now I want to add in an IOrderManager, so I have 3 different lines to adapt: I have to add a private variable to store it, add a parameter to the constructor, and add a line inside the constructor to set the private variable. 现在,我想添加一个IOrderManager,所以我要适应3条不同的行:我必须添加一个私有变量来存储它,向构造函数添加一个参数,并在构造函数内添加一行以设置私有变量。

I'd love to be able to write a macro/template/snippet that would allow me to enter "IOrderManager" and have it add all the code for me. 我希望能够编写一个宏/模板/代码段,使我可以输入“ IOrderManager”并为其添加所有代码。 Is there any way I can do this in VS2013 or Resharper? 有什么办法可以在VS2013或Resharper中实现?

The easiest thing is to add the field manually, then Alt + Enter and let ReSharper initialise the field from the constructor. 最简单的方法是手动添加字段,然后按Alt + Enter ,然后让ReSharper从构造函数中初始化该字段。 It will add the parameter, and initialise the field with it. 它将添加参数,并使用它初始化字段。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM