18

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
  • 39,424
  • 5
  • 49
  • 62
Craig
  • 195
  • 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

54
   this.GetType().Name

should return a Class name

taffarel
  • 4,015
  • 4
  • 33
  • 61
6

This should do:

this.GetType().ToString()
Oded
  • 489,969
  • 99
  • 883
  • 1,009