You can just use is . But note that is does not do the same as IsSubclassOf . See Jeppe for a great comment for details, and I also have an example below.
On the side of the note, I donโt think Java allows the equivalent of instanceof in this general case for some reason, but this is normal in C #. I.e:.
public static bool IsChildOf<T>(this object obj) { return obj is T; }
Then this makes it so trivial that it is more confusing for readers to use the extension method than is directly. If you used it directly, your test will look like this:
[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); }
source share