I’m not sure if this is possible or not, but what I would like to be able to do is to have all Children of a Parent base class to run a method defined in the base class without explicitly invoking it in the child class.
In other words, what I’d like to achieve is something like this:
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
}
}
Is there a pattern that could enable this behaviour, or could perhaps Aspect Oriented Programming be a way to achieve this?
You could instead override the
Form.OnLoadmethod of the parent form; it’s guaranteed to be called just before the Form is shown.