我如何生成使用Visual Studio(和/或ReSharper的)类字段的构造? [英] How do I generate a constructor from class fields using Visual Studio (and/or ReSharper)?

查看:275
本文介绍了我如何生成使用Visual Studio(和/或ReSharper的)类字段的构造?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经习惯把很多的Java IDE(Eclipse中,NetBeans中的IntelliJ)为您提供一个命令生成基于类中的字段的类的默认构造函数的。

I've gotten accustomed to many of the Java IDEs (Eclipse, Netbeans, IntelliJ) providing you with a command to generate a default constructor for a class based on the fields in the class.

例如:

public class Example
{
    public decimal MyNumber { get; set; }
    public string Description { get; set; }
    public int SomeInteger { get; set; }

    // ↓↓↓ This is what I want generated ↓↓↓
    public Example(decimal myNumber, string description, int someInteger)
    {
        MyNumber = myNumber;
        Description = description;
        SomeInteger = someInteger;
    }
}

有一个构造函数填充所有对象的字段在大多数OOP汉语语言这样一个共同的任务,我假设有一个某种方式为我节省时间写在C#这个样板code。我是新的C#的世界,所以我想知道如果我想的东西关于语言基础?有没有在Visual Studio中的一些选项是显而易见的?

Having a constructor populate the all of the fields of an object is such a common task in most OOP langauges, I'm assuming that there is a some way for me to save time writing this boilerplate code in C#. I'm new to the C# world, so I'm wondering if I'm missing something fundamental about the language? Is there some option in Visual Studio that is obvious?

推荐答案

ReSharper的提供<一个href=\"http://www.jetbrains.com/resharper/features/$c$c_generation.html#Generate_Type_Constructor\">Generate构造工具,您可以选择任何领域/你想要初始化属性。我用的是<大骨节病>替代 + <大骨节病>插件热键访问此。

Resharper offers a Generate Constructor tool where you can select any field/properties that you want initialized. I use the Alt+Ins hot-key to access this.

这篇关于我如何生成使用Visual Studio(和/或ReSharper的)类字段的构造?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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