I have a base class that has a method that gets executed by derived classes.
The method is raised by a constructor of the derived class and by some methods or properties in it.
I need to determine if that came from inside the instance constructor of that derived class or after that (in runtime).
The following example explains what I need:
public class Base
{
public Base()
{
}
protected void OnSomeAction(object sender)
{
// if from derived constructor EXIT, else CONTINUE
}
}
public class Derived : Base
{
public void Raise()
{
base.OnSomeAction(this); // YES if not called by constructor
}
public Derived()
{
base.OnSomeAction(this); // NO
Raise(); // NO
}
}
class Program
{
static void Main(string[] args)
{
var c = new Derived(); // NO (twice)
c.Raise(); // YES
}
}
The problem is that I cannot change the signature or arguments because I cannot alter derived classes. Basically what I was thinking is to determine if the derived class (sender) is fully constructed.
So the implementation is as is. I cannot do changes in the base class that break derived classes. I can do changes only to the base class :/
Is this possible in some way, good or not? Even some reflection magic or similar hacky approach is unfortunately welcome as this is a must :/.
Thanks!
Yes
Not. But you already knew that.
Nevertheless, here is one way to do it.
source