如何自动生成一个免费接收和存储服务的构造函数? [英] How can I automatically generate a constructor that receives and stores services for free?

查看:62
本文介绍了如何自动生成一个免费接收和存储服务的构造函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我经常发现自己手动键入如下代码:

I regularly find myself manually typing out code that looks like this:

public class SomeClass
{
    readonly ServiceA serviceA;
    readonly ServiceB serviceB;

    public SomeClass(ServiceA serviceA, ServiceB serviceB)
    {
        this.serviceA = serviceA.;
        this.serviceB = serviceB;
    }
}



要求



我想通过生成尽可能多的这种方式来节省时间。我不想使用需要购买的工具。这里唯一可变的信息是类名称和服务类型。因此,在此示例中,我想键入以下最小内容:

Requirements

I would like to save time by having a way to generate as much of this as possible. I don't want to use a tool that requires a purchase. The only information that is variable here is the class name and the types of the services. So in this example, I would like to be to type something minimal like this:


  1. Some + 快捷方式

  2. SomeClass

  3. ServiceA

  4. ServiceB

  5. Enter

  1. Some+Shortcut
  2. SomeClass
  3. ServiceA
  4. ServiceB
  5. Enter

我也愿意接受一个仍然需要我输入字段名称的解决方案。我不介意在字段定义中是否存在 private 访问修饰符,尽管我宁愿不使用它,因为这样可以使代码更加精简。同样,我愿意接受不生成只读字段的解决方案,但我更喜欢它们,因为在类初始化后我很少更改服务。

I'm also open to a solution that still requires me to type in the names of the fields. I don't mind whether or not the private access modifier is present in the field definitions, although I prefer to not have it since the code is a bit leaner that way. Similarly, I'm willing to accept a solution that doesn't generate read-only fields, but I do prefer them since I seldom want to change a service after my class is initialised.

目前我所知道的最快的解决方案是在另一部分代码中键入以下内容,并告诉Visual Studio

The quickest solution I know of at the moment is to type something like the following in another section of code and tell Visual Studio to create the class and constructor from its usage.

new SomeClass((ServiceA)null, (ServiceB)null);

但是,我并非总是以此顺序工作;有时我想在使用它之前创建一个类。因此,我通常这样做是:

However, I don't always work in this order; sometimes I want to create a class before using it. So what I typically do is this:


  1. 调用 ctor 代码段。 / li>
  2. 填充构造函数主体。

  3. 使用CodeRush Xpress生成字段。 (它提供了一种生成只读字段的方法,而Visual Studio不会将它们设置为只读。)

  1. Invoke the ctor code snippet.
  2. Fill in the constructor body.
  3. Use CodeRush Xpress to generate the fields. (It provides a way to generate read-only fields, whereas Visual Studio doesn't make them read-only.)

代码-代码段效果很好,但我认为它们不支持可变数量的参数,因此它们可能不适合此问题。

Code-snippets work well, but I don't think they support variable numbers of parameters, so they're probably not suited to this problem.

推荐答案

转到默认的C#代码段位置,通常在以下位置:C:\Program Files(x86)\Microsoft Visual Studio 10.0\VC#\Snippets\1033\Visual C#并将 ctor.snippet 的副本复制到您的个人代码段位置,对其进行重命名,并为其指定适当的名称和快捷键。然后更改声明(查看其他现有的声明),如下所示即可完成工作。创建它之后,您只需在一个空的类文件中键入 svc + TAB (或选择的任何快捷方式,然后按Tab),就可以填充内容,或者您也许可以自定义类模板,以便在添加新模板时可以选择新模板。

Go to your default C# code snippets location, usually in C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC#\Snippets\1033\Visual C# and make a copy of ctor.snippet into your personal code snippets location, rename it, and give it a proper name and shortcut key. Then change the declaration (look at other existing ones), something like the below can do the job. Once you create it, you can simply type svc + TAB (or whatever shortcut you choose then tab) in an empty class file, and you should get the content filled up, alternatively you may be able to customize a class template so when you do add new, you can select your new template.

<?xml version="1.0" encoding="utf-8" ?>
<CodeSnippets  xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
    <CodeSnippet Format="1.0.0">
        <Header>
            <Title>Some Constructor</Title>
            <Shortcut>svc</Shortcut>
            <Description>my description</Description>
            <Author>Jason was here</Author>
            <SnippetTypes>
                <SnippetType>Expansion</SnippetType>
            </SnippetTypes>
        </Header>
        <Snippet>
            <Declarations>
                <Literal>
                    <ID>accessor</ID>
                    <ToolTip>Access modifier</ToolTip>
                    <Default>public</Default>
                </Literal>
                <Literal Editable="false">
                    <ID>classname</ID>
                    <ToolTip>Class name</ToolTip>
                    <Function>ClassName()</Function>
                    <Default>ClassNamePlaceholder</Default>
                </Literal>
                <Literal Editable="true">
                    <ID>svcA</ID>
                    <ToolTip>Service A</ToolTip>
                    <Default>ServiceA</Default>
                </Literal>
                <Literal Editable="true">
                    <ID>svcAvar</ID>
                    <ToolTip>Service A</ToolTip>
                    <Default>serviceA</Default>
                </Literal>
                <Literal Editable="true">
                    <ID>svcB</ID>
                    <ToolTip>Service B</ToolTip>
                    <Default>ServiceB</Default>
                </Literal>
                <Literal Editable="true">
                    <ID>svcBvar</ID>
                    <ToolTip>Service B</ToolTip>
                    <Default>serviceB</Default>
                </Literal>
            </Declarations>
            <Code Language="csharp"><![CDATA[readonly $svcA$ $svcAvar$;
    readonly $svcB$ $svcBvar$;

    $accessor$ $classname$ ($svcA$ serviceA, $svcB$ serviceB)
    {
        this.$svcAvar$ = serviceA;
        this.$svcBvar$ = serviceB
    }$end$]]>
            </Code>
        </Snippet>
    </CodeSnippet>
</CodeSnippets>

在上面,我们声明了类 svcA = ServiceA svcB = ServiceB 和类变量 svcAvar = serviceA svcBvar = serviceB ,因此我们可以轻松地在一个位置进行修改,可以在构造函数中为参数创建更多内容,等等,但应该足以让您入门。

In the above we declare variables for the classes svcA = ServiceA, svcB = ServiceB, and the class variables svcAvar = serviceA and svcBvar = serviceB, so we can easily modify in one location, you can create more for the params in the constructor, etc, but should be enough to get you started.

关于可变数量的参数,您可以使用默认的params文字,然后在插入ctor之后可以键入所需的任何参数,如果您具有变化的类级变量,则就像其他人所说的那样,使用不同的快捷方式创建多个代码段,例如 svc svc1 svc2 svc3

Regarding the variable number of params, you can use the default params literal which would then let you type whatever params you need after you insert the ctor, if you have varying class level variables, then just like others have said, create several snippets with different shortcuts, like svc, svc1, svc2, svc3 etc

<Literal>
    <ID>params</ID>
    <ToolTip>Parameter list</ToolTip>
    <Default></Default>
</Literal>

然后 ...<![CDATA [$ accessor $ $ classname $(... $ params $)

这篇关于如何自动生成一个免费接收和存储服务的构造函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆