减少继承构造函数参数列表的输入开销

Reducing typing overhead for an inherited constructor's parameter list

是否有任何语法 and/or 语言功能可以减少在继承的构造函数中重新键入参数列表的开销?

基础Class

public class Cartesian
{
    public int X { get; set; }
    public int Y { get; set; }

    public Cartesian(int x, int y)
    {
        X = x;
        Y = y;
    }
}

继承Class

public class Complex : Cartesian
{
    public int I { get; set; }

    // all this retyping becomes daunting and results in duplicate code
    public Complex(int x, int y, int i) : base(x, y)
    {
        I = i;
    }
}

这是一种解决方法,但它只是将重复代码移动到 Init 函数中,所以...:[=​​13=]

基础Class

public class Cartesian
{
    public int X { get; set; }
    public int Y { get; set; }

    //public Cartesian(int x, int y) { Init(x, y); }

    public void Init(int x, int y)
    {
        X = x;
        Y = y;        
    }
}

继承Class

public class Complex : Cartesian
{
    public Cartesian Base { get {return base;} }

    public int I { get; set; }

    //public Complex(int i) { Init(i); }

    public Complex Init(int i)
    {
        I = i;

        return this;
    }
}

用法示例

var complex = (new Complex()).Init(i).Base.Init(x, y);

如果您拥有 ReSharper 8,则有一项名为 Generative Completion 的功能。

One form of code completion that made its appearance in ReSharper 8 is called Generative Completion. The idea of generative completion is to use code completion for code generation as a quicker, more direct alternative to, say, the Generate menu.

换句话说,键入 ctorp 并按 Tab 键生成 ctor

public class Complex : Cartesian
{
    public int I { get; set; }

    ctorp//tab key
}

生成

public Complex(int x, int y) : base(x, y)
{
     I = x;
}

您仍然需要填写派生的 class 成员并进行分配,但这会减少开销。

来自P.Brian.Mackey的回答,如果ctorp被关键字替换,说base.ctorp:

public class Complex : Cartesian
{
    public int I { get; set; }

    //public Complex(int x, int y, int i) : base(x, y)
    public Complex(int i) : base.ctorp
    {
        I = i;
    }
}

然后让编译器根据签名确定调用哪个构造函数并相应地替换 ctorp(并且可能有一些解释性符号来补充):

var Complex = new Complex((x, y), i);

生成

    //public Complex(int i) : base.ctorp
    public Complex(int x, int y, int i) : base(x, y)
    {
        I = i;
    }

var Complex = new Complex(i);

生成

    //public Complex(int i) : base.ctorp
    public Complex(int i) : base()
    {
        I = i;
    }