jeudi 21 février 2019

Design pattern for overcoming the reverse constructor order?

I have the following problem:

The base class expects to receive some data but the data is initialized by the derived class constructor which in C# is called after the base constructor was called.

Simplified example:

class Base
{
    string _s;

    protected Base(string s)
    {
        _s = s.ToLower();
    }
}

class Derived : Base
{
    public Derived(string path) : base(...)
    {
        // string can only be initialized at this point
    }
}

Question:

How can I run some code in derived class first, so I can pass the result to the base constructor ?

Aucun commentaire:

Enregistrer un commentaire