The Wiert Corner – irregular stream of stuff

Jeroen W. Pluimers on .NET, C#, Delphi, databases, and personal interests

  • My badges

  • Twitter Updates

  • My Flickr Stream

  • Pages

  • All categories

  • Enter your email address to subscribe to this blog and receive notifications of new posts by email.

    Join 1,860 other subscribers

.NET/C#: Type.IsSubclassOf Method versus Type.IsAssignableFrom

Posted by jpluimers on 2013/10/22

Every once in a while, you will have to do is checks using reflection.

Basically there are two methods:

Thanks to Ani and Werner Beroux for explaining the difference in more detail on StackOverflow, I added some extra links:

To check for assignability, you can use the Type.IsAssignableFrom method:

typeof(SomeType).IsAssignableFrom(typeof(Derived))

This will work as you expect for

but not when you are looking for ‘assignability’ across explicit / implicit conversion operators.

To check for strict inheritance, you can use Type.IsSubclassOf:

typeof(Derived).IsSubclassOf(typeof(SomeType))

–jeroen

via: c# – How to check if a class inherits another class without instantiating it? – Stack Overflow.

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.