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


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


source


Use Type.IsAssignableFrom :



if (typeof(baseclass).IsAssignableFrom(inh.GetType())
{
...
}

      

+3


source


You can also use Type.IsSubclassOf

inherited.GetType().IsSubclassOf(typeof(Base));

      

+3


source







All Articles