How do I check if a type is a subtype OR the type of an object?

Apparently, no. Here’s the options: Use Type.IsSubclassOf Use Type.IsAssignableFrom is and as Type.IsSubclassOf As you’ve already found out, this will not work if the two types are the same, here’s a sample LINQPad program that demonstrates: void Main() { typeof(Derived).IsSubclassOf(typeof(Base)).Dump(); typeof(Base).IsSubclassOf(typeof(Base)).Dump(); } public class Base { } public class Derived : Base { } Output: … Read more

How to find all the subclasses of a class given its name?

New-style classes (i.e. subclassed from object, which is the default in Python 3) have a __subclasses__ method which returns the subclasses: class Foo(object): pass class Bar(Foo): pass class Baz(Foo): pass class Bing(Bar): pass Here are the names of the subclasses: print([cls.__name__ for cls in Foo.__subclasses__()]) # [‘Bar’, ‘Baz’] Here are the subclasses themselves: print(Foo.__subclasses__()) # … Read more

How do you find all subclasses of a given class in Java?

Scanning for classes is not easy with pure Java. The spring framework offers a class called ClassPathScanningCandidateComponentProvider that can do what you need. The following example would find all subclasses of MyClass in the package org.example.package ClassPathScanningCandidateComponentProvider provider = new ClassPathScanningCandidateComponentProvider(false); provider.addIncludeFilter(new AssignableTypeFilter(MyClass.class)); // scan in org.example.package Set<BeanDefinition> components = provider.findCandidateComponents(“org/example/package”); for (BeanDefinition component : … Read more