How to find the child class name from base class?

60,577

Solution 1

Get the type of the current object, then its name.

this.GetType().Name

Solution 2

Try this:

Type type = this.GetType().UnderlyingSystemType;  
String className = type.Name;  

Solution 3

If you call this.GetType() you'll always get the current runtime type regardless of the base class you're inheriting from.

Share:
60,577
Xaqron
Author by

Xaqron

Physician

Updated on June 14, 2021

Comments

  • Xaqron
    Xaqron almost 3 years

    At run-time, inside base class, how to find the current child class name ?

  • Andrew Barber
    Andrew Barber over 11 years
    Welcome to Stack Overflow! Thanks for posting your answer! Please be sure to read the FAQ on Self-Promotion carefully. Also note that it is required that you post a disclaimer every time you link to your own site/product.
  • markg
    markg over 11 years
    thanks Andrew for letting me know. :) my blog site though is just my personal note because I tend to forget things.. nothing about promotion or getting user hits. not even worried if someone would view or comment on it. will take note of the rules. many thanks! :)
  • Michi-2142
    Michi-2142 over 9 years
    For base class you can use: this.GetType().BaseType.Name
  • alikuli
    alikuli about 6 years
    It doesn't seem to work for partial classes. You get a GUID type number added after the name of the Class.