Reflection: Inherited Class Static Method

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
 
J

Jon Skeet [C# MVP]

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.
 
N

Nicole Calinoiu

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.
 
J

James Hancock

damn. Ok. Thanks guys! I was hoping :) Doesn't do too much to it though,
so I'll live.

James Hancock
 
J

Jako Menkveld

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?
 
J

Jako Menkveld

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
 
J

Jako Menkveld

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?
 
J

James Hancock

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 :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top