Find current class from within a static method

J

John Mott

Hi all,

Is there a way to get the name of the current class in a static method? I
have a base class that contains static methods and that class is inherited.
I'd like to change the behavior in the derived class but static methods
cannot be overridden. If I can find out 'who i am' i can do what I need to
do.

thanks,

john
 
J

Jon Skeet [C# MVP]

John Mott said:
Is there a way to get the name of the current class in a static method? I
have a base class that contains static methods and that class is inherited.
I'd like to change the behavior in the derived class but static methods
cannot be overridden. If I can find out 'who i am' i can do what I need to
do.

I'm afraid not - there's no real idea of the "current class" in the
context of a static method.
 
G

Guest

Is the following similar to what you wish to achieve, such that
Animal.LegCount and Lion.LegCount return differing logic?

public class Animal
{
public static int LegCount()
{
return 0;
}
}

public class Lion : Animal
{
public new static int LegCount()
{
return 4;
}
}
 
J

John Mott

That is defintely an improvement over what I have now. I have multiple
static routines but this scheme could reduce the number requires in the
derived class to just what it has to 'know', then it can manually call the
base class routines.

thanks!

john
 
C

Carlos S

I would suggest going away from static methods in this case since what you're looking for is polymorphic behavior. I would make the method an instance method and mark it as virtual in the base class and override it in the subclass.

Carlos S
 
J

John Mott

I'm with you on the behavior and many of the current methods are virtual and
overridden for the instantiated objects, but its syntactically and logically
convenient to use static methods -- they retrieve instances of the class
through a query, so they are operating like a factory:

DataTable dt = Prospects.Retrieve("state = 'TN'"); // retrieve all
Prospects where the State is Tennessee

Right now the static methods are declared on the derived classes, but I can
push these into the base class and just pass in minor information. The goal
is tidyness, more logic in the common base class and less in the derived
class.

john
 
D

Doug Forster

Hi John,
Right now the static methods are declared on the derived classes, but I
can push these into the base class and just pass in minor information. The
goal is tidyness, more logic in the common base class and less in the
derived class.

Have you looked at custom attributes? A very neat way of adding static meta
data to a class. Then you just need to pass the type into the base class
static methods and they get the rest from the custom attributes.

Cheers
Doug Forster
 
S

Stanimir Stoyanov

John Mott said:
Hi all,

Is there a way to get the name of the current class in a static method? I
have a base class that contains static methods and that class is
inherited. I'd like to change the behavior in the derived class but static
methods cannot be overridden. If I can find out 'who i am' i can do what I
need to do.

thanks,

john

I personally use the StackTrace class [1] to obtain such information. It is
the same approach used by Exception objects in order to provide more
information. See the following example:

using System.Diagnostics;
....

StackTrace st = new StackTrace(new StackFrame(false));
StackFrame sf = st.GetFrame(0); // Get the most top stack frame

Console.WriteLine(sf.GetMethod().DeclaringType);

The sample application will output the name of the type in which the
currently executed method resides. By comparing the type with your base or
derived classes you can call different routines or functions.

Best Regards,
Stanimir Stoyanov
www.stoyanoff.info | www.aeroxp.org
 
J

John Mott

wow. That looks promising. Thanks!

john

Doug Forster said:
Hi John,


Have you looked at custom attributes? A very neat way of adding static
meta data to a class. Then you just need to pass the type into the base
class static methods and they get the rest from the custom attributes.

Cheers
Doug Forster
 
J

John Mott

Very cool, thanks!

john
Stanimir Stoyanov said:
John Mott said:
Hi all,

Is there a way to get the name of the current class in a static method? I
have a base class that contains static methods and that class is
inherited. I'd like to change the behavior in the derived class but
static methods cannot be overridden. If I can find out 'who i am' i can
do what I need to do.

thanks,

john

I personally use the StackTrace class [1] to obtain such information. It
is the same approach used by Exception objects in order to provide more
information. See the following example:

using System.Diagnostics;
...

StackTrace st = new StackTrace(new StackFrame(false));
StackFrame sf = st.GetFrame(0); // Get the most top stack frame

Console.WriteLine(sf.GetMethod().DeclaringType);

The sample application will output the name of the type in which the
currently executed method resides. By comparing the type with your base or
derived classes you can call different routines or functions.

Best Regards,
Stanimir Stoyanov
www.stoyanoff.info | www.aeroxp.org
 
D

Doug Semler

Stanimir Stoyanov said:
John Mott said:
Hi all,

Is there a way to get the name of the current class in a static method? I
have a base class that contains static methods and that class is
inherited. I'd like to change the behavior in the derived class but
static methods cannot be overridden. If I can find out 'who i am' i can
do what I need to do.

thanks,

john

I personally use the StackTrace class [1] to obtain such information. It
is the same approach used by Exception objects in order to provide more
information. See the following example:

using System.Diagnostics;
...

StackTrace st = new StackTrace(new StackFrame(false));
StackFrame sf = st.GetFrame(0); // Get the most top stack frame

Console.WriteLine(sf.GetMethod().DeclaringType);

The sample application will output the name of the type in which the
currently executed method resides. By comparing the type with your base or
derived classes you can call different routines or functions.


Be warned: Stack walks can be (are?) VERY Expensive.

--
Doug Semler, MCPD
a.a. #705, BAAWA. EAC Guardian of the Horn of the IPU (pbuhh).
The answer is 42; DNRC o-
Gur Hfrarg unf orpbzr fb shyy bs penc gurfr qnlf, abbar rira
erpbtavmrf fvzcyr guvatf yvxr ebg13 nalzber. Fnq, vfa'g vg?
 

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