C# How to execute code after object construction (postconstruction)

后端 未结 9 1935
自闭症患者
自闭症患者 2020-12-18 17:32

As you can see in the code below, the DoStuff() method is getting called before the Init() one during the construction of a Child object.

I\'m in a situation where I

9条回答
  •  旧巷少年郎
    2020-12-18 18:25

    I would strongly suggest use Factory like a pattern.

    If it's possible:

    1. Push all your childs and abstract class into separate assembly.

    2. Declare ctors of childs like internal methods, so no one out of that assembly is can construct them just by calling ctor.

    3. Implement the Factory class to construct for caller specified objects type, which obviuoly will forse calling of abstract DoStuff() method after actual creation of anobject, but before returning it to caller.

    Good thing about this is that: It will give you also additional level of abstraction, so if in the future you will need some more functions call or any other type of logical complexity, what you will need, is just add them into your Factory class.

    That is.

    Regards

提交回复
热议问题