You can just use is
. But note that is
doesn't do exactly the same as IsSubclassOf
. See Jeppe's excellent comment for details and I have an example below too.
On a side note, I don't think Java allows the equivalent instanceof
in such a generic case for some reason, but it's OK in C#. I.e.:
public static bool IsChildOf<T>(this object obj)
{
return obj is T;
}
Then this makes it so trivial that it's more confusing to readers to use an extension method than is
directly. If you used it directly your test would then look like:
[Test()]
public void IsChildOfTest()
{
var dog = new Dog();
var isAnimal = dog is Animal;
Assert.That(isAnimal);
}
An example of one of the differences between is
and IsSubclassOf
:
[Test]
public void IsChildOfTest()
{
var dog = new Dog();
Assert.False(dog.GetType().IsSubclassOf(typeof(Dog)));
Assert.True(dog is Dog);
}