17

I'm trying to get a current class name into a string.

For example:

public class Marker : Mark
{
    string currentclass = ???;
}

public abstract class MiniMarker : Mark
{
}

I'd like to get the string from Marker class so I do not have to put it inside each abstract class I make from it.

I want the string to be MiniMarker, or what ever the abstract class is named.

I tried MethodBase.GetCurrentMethod().DeclaringType, but it did not work.

Rostyslav Dzinko
  • 38,056
  • 5
  • 48
  • 60
Craig
  • 185
  • 1
  • 1
  • 5
  • 5
    Is your sample correct? How is Marker and MiniMarker related, Marker is based on Mark. – rene Aug 20 '12 at 09:48
  • Your example and your reference to abstract classes makes no sense. Your code belongs in the abstract base class, `Mark`. The classes derived from it are concrete unless you have a multilevel hierarchy. – Jim Balter Jan 02 '18 at 19:34

2 Answers2

52
   this.GetType().Name

should return a Class name

taffarel
  • 3,915
  • 4
  • 32
  • 58
5

This should do:

this.GetType().ToString()
Oded
  • 477,625
  • 97
  • 867
  • 998