views:

105

answers:

4

Suppose we have 2 classes, Child, and the class from which it inherits, Parent.

class Parent
{
    public static void MyFunction(){}
}

class Child : Parent
{
}

Is it possible to determine in the parent class how the method was called? Because we can call it two ways:

Parent.MyFunction();
Child.MyFunction();

My current approach was trying to use:

MethodInfo.GetCurrentMethod().ReflectedType; // and
MethodInfo.GetCurrentMethod().DeclaringType;

But both appear to return the Parent type.

If you are wondering what, exactly I am trying to accomplish (and why I am violating the basic OOP rule that the parent shouldn't have to know anything about the child), the short of it is this (let me know if you want the long version):

I have a Model structure representing some of our data that persists to the database. All of these models inherit from an abstract Parent. This parent implements a couple of events, such as SaveEvent, DeleteEvent, etc. We want to be able to subscribe to events specific to the type. So, even though the event is in the parent, I want to be able to do:

Child.SaveEvent += new EventHandler((sender, args) => {});

I have everything in place, where the event is actually backed by a dictionary of event handlers, hashed by type. The last thing I need to get working is correctly detecting the Child type, when doing Child.SaveEvent.

I know I can implement the event in each child class (even forcing it through use of abstract), but it would be nice to keep it all in the parent, which is the class actually firing the events (since it implements the common save/delete/change functionality).

+5  A: 

No - calling Child.MyFunction() results in IL which calls Parent.MyFunction() directly. The resulting binary is basically indistinguishable.

Jon Skeet
well darn. Oh master of the C# events, are there any tricks to doing typed events, using the EventHandler Event {add{} remove{}} approach?
Matt
A: 

The last thing I need to get working is correctly detecting the Child type, when doing Child.SaveEvent.

You could use a generic base class or make the method generic.

winSharp93
I considered the generic base class approach. But it seems .... excessive to go through and genricify all our inheritance structure for just this event implementation.
Matt
btw, I hear by claim trademark over the word genericfy! ;) If only I hadn't misspelled my first use of it.
Matt
A: 

I don't think that this is possible, for the simple reason that Child.SaveEvent doesn't really exist. SaveEvent is only declared in Parent, though the compiler allows you to call it as Child.SaveEvent.

To do what you need to do, you really shouldn't be using a static method. You could easily define a non-static SaveEvent that would behave exactly as needed. If you really need a static wrapper, you could use a singleton.

JSBangs
A: 

I don't know the code at all obviously, but I question the wisdom of using static methods in this case. Perhaps you need a separate "Manager" class which is a singleton which goes the event stuff as well as other co-ordination tasks for all the model objects.

tster