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.
Comments
-
Xaqron almost 3 years
At
run-time
, insidebase class
, how to find the current child class name ? -
Andrew Barber over 11 yearsWelcome 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 over 11 yearsthanks 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 over 9 yearsFor base class you can use:
this.GetType().BaseType.Name
-
alikuli about 6 yearsIt doesn't seem to work for partial classes. You get a GUID type number added after the name of the Class.