如何强制在C#中调用基础构造函数?

时间:2022-07-14 15:03:49

I have a BasePage class which all other pages derive from:

我有一个BasePage类,所有其他页面派生自:

public class BasePage

This BasePage has a constructor which contains code which must always run:

这个BasePage有一个构造函数,其中包含必须始终运行的代码:

public BasePage()
{
    // Important code here
}

I want to force derived classes to call the base constructor, like so:

我想强制派生类调用基础构造函数,如下所示:

public MyPage
    : base()
{
    // Page specific code here
}

How can I enforce this (preferably at compile time)?

我该如何强制执行此操作(最好是在编译时)?

4 个解决方案

#1


24  

The base constructor will always be called at some point. If you call this(...) instead of base(...) then that calls into another constructor in the same class - which again will have to either call yet another sibling constructor or a parent constructor. Sooner or later you will always get to a constructor which either calls base(...) explicitly or implicitly calls a parameterless constructor of the base class.

始终会在某个时刻调用基础构造函数。如果你调用this(...)而不是base(...)那么它会调用同一个类中的另一个构造函数 - 这又需要调用另一个兄弟构造函数或父构造函数。迟早你总会得到一个构造函数,它可以显式地或隐式地调用base(...)来调用基类的无参数构造函数。

See this article for more about constructor chaining, including the execution points of the various bits (such as variable initializers).

有关构造函数链接的更多信息,请参阅此文章,包括各个位的执行点(例如变量初始值设定项)。

#2


8  

The base class constructor taking no arguments is automatically run if you don't call any other base class constructor taking arguments explicitly.

如果不显式调用任何其他基类构造函数,则自动运行不带参数的基类构造函数。

#3


3  

The base class constructor is always called, even if you don't call it explicitly. So you don't need to do any extra work to make sure that happens.

即使您没有显式调用它,也始终会调用基类构造函数。所以你不需要做任何额外的工作来确保发生这种情况。

#4


1  

One of the base constructors always needs to be called, and the default one is called when the base constructor is not explicitly stated.

总是需要调用其中一个基本构造函数,并且在未明确声明基本构造函数时调用默认构造函数。

Edit: rephrased for clarity.

编辑:重新说明清楚。

#1


24  

The base constructor will always be called at some point. If you call this(...) instead of base(...) then that calls into another constructor in the same class - which again will have to either call yet another sibling constructor or a parent constructor. Sooner or later you will always get to a constructor which either calls base(...) explicitly or implicitly calls a parameterless constructor of the base class.

始终会在某个时刻调用基础构造函数。如果你调用this(...)而不是base(...)那么它会调用同一个类中的另一个构造函数 - 这又需要调用另一个兄弟构造函数或父构造函数。迟早你总会得到一个构造函数,它可以显式地或隐式地调用base(...)来调用基类的无参数构造函数。

See this article for more about constructor chaining, including the execution points of the various bits (such as variable initializers).

有关构造函数链接的更多信息,请参阅此文章,包括各个位的执行点(例如变量初始值设定项)。

#2


8  

The base class constructor taking no arguments is automatically run if you don't call any other base class constructor taking arguments explicitly.

如果不显式调用任何其他基类构造函数,则自动运行不带参数的基类构造函数。

#3


3  

The base class constructor is always called, even if you don't call it explicitly. So you don't need to do any extra work to make sure that happens.

即使您没有显式调用它,也始终会调用基类构造函数。所以你不需要做任何额外的工作来确保发生这种情况。

#4


1  

One of the base constructors always needs to be called, and the default one is called when the base constructor is not explicitly stated.

总是需要调用其中一个基本构造函数,并且在未明确声明基本构造函数时调用默认构造函数。

Edit: rephrased for clarity.

编辑:重新说明清楚。