How to call a method as soon as the constructor is complete?

How can I call a method every time the constructor runs? In other words, inside the class, is it possible to somehow call another method as soon as the constructor is complete?

+3
source share
4 answers

You can simply call the method as the last line of the constructor.

Alternatively, if you do not own this class, you can upgrade to Aspect-Oriented Programming ( http://en.wikipedia.org/wiki/Aspect_oriented_programming )

+2
source

No. Unless you name it inside your constructor.

, Init(), , .

+2

, , ?

public class SomeMethod
{
    public SomeMethod()
    {
       anotherMethod();
    }

    public void anotherMethod()
    {
    }
}
0

, . , , , , , , , , , , . , , , .

A rather unpleasant clown hack, but, unfortunately, the only way I know for the base class constructor is to clean up any unmanaged resources that it owns if the derived class constructor throws an exception.

0
source

Source: https://habr.com/ru/post/1780854/


All Articles