virtual
methods should not be called in the constructor of a base class because the constructor of the derived class is not called and so all initialisation logic isn't invoked.
I want to know if there is a way to hook in there to automatically call a method after the object is completely constructed. I do not want to push the responsibility of calling an initialise method onto the user.
Lets say I have the following
public abstract class Foo
{
protected Foo()
{
...
AfterConstruction();
}
protected virtual void AfterConstruction(){}
}
public class Bar : Foo
{
protected override void AfterConstruction()
{
...
}
}
I know this should not be done and I thought maybe one can get around this by using reflection to observe object construction and then hook after the construction is finished to call the function AfterConstruction()
. But I cannot find appropriate code to do so.
Thank you for your thoughts
Aucun commentaire:
Enregistrer un commentaire