Reflection: Inherited Class Static Method

  • Thread starter Thread starter James Hancock
  • Start date Start date
J

James Hancock

I have a parent class that has a static method on it. Of course it's
inherited by the children which is what I want.

However, in that static method, I need to know the child that the method was
called from. (specifically the Type)

I can get the CallingAssembly but that just gives me the place where the
static method was called, not the type that the static method was called on.

Here's what my code looks like:

public class someClass {
public static void SomeMethod() {

}
}

public class someClassChild : someClass {
... more stuff here.
}


In the code it calls like this: someClassChild.SomeMethod();

In the .SomeMethod that is actually on someClass I need to know that
someClassChild was actually called. all I need is the type.

I've looked all over the reflection namespace but can't find anything. What
am I missing?

Thanks!
James hancock
 
James Hancock said:
I have a parent class that has a static method on it. Of course it's
inherited by the children which is what I want.

In the code it calls like this: someClassChild.SomeMethod();

That will get compiled as someClass.SomeMethod(), unless you actually
have a specific someClassChild.SomeMethod() method.
In the .SomeMethod that is actually on someClass I need to know that
someClassChild was actually called. all I need is the type.

I've looked all over the reflection namespace but can't find anything. What
am I missing?

You won't be able to get what you want, due to the above.
 
Actually, the static method isn't really inherited -- it's just accessible
via the subtypes. If you take a look at the IL for your compiled assembly,
you'll see that the supertype is used even there. Since the subtype isn't
actually involved at runtime, it's simply not possible to do what you want
using a static method.
 
damn. Ok. Thanks guys! I was hoping :) Doesn't do too much to it though,
so I'll live.

James Hancock
 
Just read through this and I have an idea that might help:

In your parent class, declare a protected static variable of type Type, e.g.

protected static Type myType;

In the constructor of of the parent class add this:
myType = this.GetType();

Now from your static method you can access myType to see what type the
subclass is.

You think this will work?
 
Just need to correct myself here...

Jako Menkveld said:
Just read through this and I have an idea that might help:

In your parent class, declare a protected static variable of type Type,
e.g.

you should make it private
protected static Type myType;

In the constructor of of the parent class add this:
myType = this.GetType();

Now from your static method you can access myType to see what type the
subclass is.

This is assuming your static method is protected so that you can call it
only from a subclass
 
OK. I thought about this some more and it would work. You static variable
will always have the type of the last instance created.

Why don't you pass the type as an argument to the static method?
 
That was exactly my solution :)

I just wanted to make it really really concise for 3rd party users (we allow
plugin and inheritence from our Forms and other things) thus eliminating the
type variable from the static method was a goal to make it simple.... we'll
just document it well :)
 
Back
Top