If the type of child is the type of the parent
I am trying to pass an inherited class type to a method and want to check if the type is a base class type. How can I do this since inherited.GetType () == typeof (baseclass) will return false?
+3
Michael DiLeo
source
to share
3 answers
The operator is
does this.
if (inherited is baseclass)
{
// do stuff
}
You can also use Type.BaseType
it if you want to know that this is exactly the direct parent.
+8
Matthew Haugen
source
to share
Use Type.IsAssignableFrom :
if (typeof(baseclass).IsAssignableFrom(inh.GetType())
{
...
}
+3
Alex kiselev
source
to share
You can also use Type.IsSubclassOf
inherited.GetType().IsSubclassOf(typeof(Base));
+3
Yuval Itzchakov
source
to share