How to get the current class name at runtime?

61,312

Solution 1

   this.GetType().Name

should return a Class name

Solution 2

This should do:

this.GetType().ToString()
Share:
61,312
Craig
Author by

Craig

Updated on July 26, 2022

Comments

  • Craig
    Craig almost 2 years

    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.