Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

If child type is type of parent

Tags:

c#

.net

types

I am trying to pass a an inherited class type in to a method and want to check if the type is a type of base class. How can I do this, since inherited.GetType() == typeof(baseclass) will return false?

like image 458
Michael DiLeo Avatar asked Aug 14 '14 04:08

Michael DiLeo


1 Answers

The is operator does this.

if (inherited is baseclass)
{
    // do stuff
}

You could also use Type.BaseType if you want to know that it is exactly the direct parent.

like image 117
Matthew Haugen Avatar answered Sep 20 '22 06:09

Matthew Haugen