C#:解压缩方法返回的元组以将其传递到父构造函数中,或其他修复"ISSUE: explicit constructor call"的方法



解压缩器(dotPeek(输出:

class Base {
public Base(int d, int e, int f) {
// logic
Console.WriteLine ("Base constructor");
}
}
class Child : Base {
public Child(string a, string b, string c) {
// logic before explicit call, calculating d, e, f
// ISSUE: explicit constructor call
base.u002Ector(d, e, f);
// logic after explicit call
}
}

试图将其转换为普通的c#基构造函数调用,但无法找到如何解压缩元组以将其作为参数传递给父构造函数:

class Base {
public Base(int d, int e, int f) {
// logic
Console.WriteLine ("Base constructor");
}
}
class Child : Base {
public Child(string a, string b, string c) : base(DoWorkBeforeBaseConstructor(a, b, c)) {
// logic after explicit call
}
private static (int, int, int) DoWorkBeforeBaseConstructor(string a, string b, string c) {
// logic generating response based on params

// logic before explicit call
return (1, 2, 3);
}
}

没有办法用元组来实现这一点。如果您有权访问基类,请添加另一个接受元组的构造函数:

public Base((int e, int f, int g) tuple) : this(tuple.e, tuple.f, tuple.g)
{
}

然而,在我看来,这一切都很棘手,我不确定你所做的是否符合良好的SOLID原则。我会考虑将DoWorkBeforeBaseConstructor方法移到其他地方,并将新值传递到Child类中。

相关内容

最新更新