我不確定這是否可行,但我希望能夠做的是讓所有的父類基類運行基類中定義的方法,而不在子類中明確調用它。在子構造函數的InitializeComponent完成後沒有在子類中隱式調用的方法運行
換句話說,我想實現的是這樣的:
public class Parent
{
public Parent()
{
InitializeComponent();
Setup(); // not run here if this instance is a child of parent
}
protected void Setup()
{
// code that depends on InitializeComponent being called in
// both the parent and any Child's classes
}
}
public class Child : Parent
{
public Child()
{
InitializeComponent();
//Setup(); is invoked here, without having to explicitly
// invoke in all children of Parent
}
}
是否有可能使這種行爲,或者也許可以面向方面編程是一種方式來實現這一模式?