Retrieving from inside a static method the type of the containing class???

B

Bob Rock

Hello,

is there a way of retrieving the type of the class containing a static
method from the method itself when it gets called?
I have this situation: an abstact class that defines a static method, a
class that derives from the abstract class and I need

to retrieve the type of the concrete class when calling the static method.

public abstact class MyAbstact
{
public static MyMethod()
{
// is there a way of getting the type of the class (i.e. MyConcrete) from
inside here???
}
}

public class MyConcrete : MyAbstact
{
public MyOtherMethod()
{
MyConcrete.MyMethod() // calling the static method defined in the
abstract class
}
}


Regards,
Bob Rock
 
?

=?ISO-8859-1?Q?Arne_Vajh=F8j?=

Bob said:
is there a way of retrieving the type of the class containing a static
method from the method itself when it gets called?
I have this situation: an abstact class that defines a static method, a
class that derives from the abstract class and I need

to retrieve the type of the concrete class when calling the static method.

public abstact class MyAbstact
{
public static MyMethod()
{
// is there a way of getting the type of the class (i.e. MyConcrete) from
inside here???
}
}

public class MyConcrete : MyAbstact
{
public MyOtherMethod()
{
MyConcrete.MyMethod() // calling the static method defined in the
abstract class
}
}

I don't think so.

Because you are really calling MyAbstract.MyMethod ...

You can get MyAbstract with:

MethodInfo.GetCurrentMethod().DeclaringType.FullName

But that is rather pointless.

Arne
 
D

Dave Sexton

Hi Bob,

No. For that requirement you should either make MyMethod an instance
method, accept a Type parameter or make it generic in the 2.0 framework if
that would add any value.
 
B

Bob Rock

Yes, that I already tried and unfortunately the abstract class and not the
concrete one is returned. Even navigating the call stack does not help.
Again the abstract type and not the concrete one is returned.

Bob Rock
 
B

Bob Rock

Yes, that is something I was trying to avoid ... it would dirty my otherwise
perfect design :-(

Bob Rock
 
B

Ben Voigt

public class MyConcrete : MyAbstact
{
public MyOtherMethod()
{
MyConcrete.MyMethod() // calling the static method defined in the
abstract class

The use of MyConcrete doesn't survive the compiler's name resolution pass.
Check the MSIL.
 
D

Dave Sexton

Hi Bob,

I'm not sure what it is that you're doing, but if your method relies on the
Type of a derived class then your design probably isn't perfect to begin
with ;)
 

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