How do you get a collection of all the types that inherit from a specific other type?
4 Answers
Something like:
public IEnumerable<Type> FindDerivedTypes(Assembly assembly, Type baseType)
{
return assembly.GetTypes().Where(t => baseType.IsAssignableFrom(t));
}
If you need to handle generics, that gets somewhat trickier (e.g. passing in the open List<>
type but expecting to get back a type which derived from List<int>
). Otherwise it's simple though :)
If you want to exclude the type itself, you can do so easily enough:
public IEnumerable<Type> FindDerivedTypes(Assembly assembly, Type baseType)
{
return assembly.GetTypes().Where(t => t != baseType &&
baseType.IsAssignableFrom(t));
}
Note that this will also allow you to specify an interface and find all the types which implement it, rather than just working with classes as Type.IsSubclassOf
does.

- 1,421,763
- 867
- 9,128
- 9,194
-
2Thanks! I ended up using this Thanks - I ended up using this public static List
GetAllSubclassesOf(Type baseType) { return Assembly.GetAssembly(baseType).GetTypes(). Where(type => type.IsSubclassOf(baseType)). ToList(); } – aceinthehole Aug 12 '09 at 21:18 -
@Downvoter here. My comment went missing. As indicated by aceinthehole the answer is incorrect. I've added the correct answer as I missed aces comment until I found the code did not work. Merry Christmas. – Tim Murphy Dec 25 '10 at 08:00
-
@Tim: It's not incorrect, it's slightly different. `IsAssignableFrom` should work as well. In particular, my version copes with interfaces as well. Did you test my code? What case does my answer fail on? – Jon Skeet Dec 25 '10 at 08:23
-
@Jon: Using LinqPad on Exception your method gives 109 types, my corrected answer gives 108. The reason is your method will include Exception in this result. I believe the question, as I needed, wants what types that inherit from baseType, not the baseType as well. – Tim Murphy Dec 25 '10 at 08:50
-
@Tim: What does yours give for `IDisposable` for example? If the OP wants to exclude the type itself, it's trivial to do so - I'll edit my answer to add that as an option. – Jon Skeet Dec 25 '10 at 08:55
-
1@Jon. Quite right your method handles interfaces. I'm a VB programmer, we inherit classes and implement interfaces. Is this different in C#? I ask because the question, as I required, wants types that inherit a type not implement a type. – Tim Murphy Dec 25 '10 at 09:02
-
2@Tim: In C# we just use " : " for both... but I would generally consider interfaces as part of the inheritance hierarchy of a type. – Jon Skeet Dec 25 '10 at 09:04
-
@JonSkeet: is there a similar Linq query for interfaces? The pseudo code I am looking for is `.IsAssignableFrom(IEnumerable)`. I am implementing your suggestion (http://stackoverflow.com/a/298994/837623) and I am somehow stuck in this scenario. Having it within the dictionary (as regular types) might not work as well but I can have a special check just before. – Moslem Ben Dhaou Apr 04 '14 at 10:16
-
Using `typeof()` will return the concrete type which I don't care about. The subsequent `Action` is expecting an `IEnumerable
` (`int` as a sample) – Moslem Ben Dhaou Apr 04 '14 at 10:19 -
@MoslemBenDhaou: It's not entirely clear what you mean. I suggest you ask a new question with more details. You should be aware that `IEnumerable` and `IEnumerable
` are separate types though. – Jon Skeet Apr 04 '14 at 10:38 -
@JonSkeet: Here is it: http://stackoverflow.com/questions/22860959/c-sharp-switch-between-types-including-interfaces – Moslem Ben Dhaou Apr 04 '14 at 11:02
-
According to this answer, a class that implements an interface "derives" from that interface. I don't think so. – HappyNomad Oct 25 '16 at 08:40
-
@HappyNomad: Are you just objecting to the naming of the method? – Jon Skeet Oct 25 '16 at 10:16
-
@JonSkeet The method's name matches the question, so it's fine. The problem is what the method does when given an interface. – HappyNomad Oct 25 '16 at 10:31
-
@HappyNomad: Well the OP never said (4 years ago...) what they'd want to happen for interfaces. Maybe only return interfaces? Maybe return all implementations? – Jon Skeet Oct 25 '16 at 10:32
-
@JonSkeet That's true. Here's [another idea](http://www.randomhaiku.com/) for what it could return when given an interface. Seriously, though, the OP *did* say, but not specifically. The OP didn't specifically mention classes, either. – HappyNomad Oct 25 '16 at 10:52
-
@HappyNomad: I suggest if you have a better answer, you add it. But I suspect this is what the OP actually wanted, to be honest. – Jon Skeet Oct 25 '16 at 10:56
-
Since @JonSkeet himself suggested it, I've added [my answer](http://stackoverflow.com/a/40238277). The OP seems to indicate that he's not concerned with interfaces. But we are! ;) – HappyNomad Oct 25 '16 at 11:10
The following method will get a collection of types that inherit a type.
C#
public IEnumerable<Type> FindSubClassesOf<TBaseType>()
{
var baseType = typeof(TBaseType);
var assembly = baseType.Assembly;
return assembly.GetTypes().Where(t => t.IsSubclassOf(baseType));
}
VB.NET
Public Function FindSubClasses(Of TBaseType)() As IEnumerable(Of Type)
Dim baseType = GetType(TBaseType)
Dim assembly = baseType.Assembly
Return From t In assembly.GetTypes()
Where t.IsSubClassOf(baseType)
Select t
End Function
If you need to include types that implement an interface see @Jon Skeet's answer.

- 4,892
- 4
- 40
- 48
You have to enumerate all types and check for each if it inherits the one you're looking for.
Some code like the one in this question may be useful for you.

- 1
- 1

- 4,635
- 4
- 32
- 31
Consider using this method (written for a PCL):
public IEnumerable<Type> FindDerivedTypes( Assembly assembly, Type baseType )
{
TypeInfo baseTypeInfo = baseType.GetTypeInfo();
bool isClass = baseTypeInfo.IsClass, isInterface = baseTypeInfo.IsInterface;
return
from type in assembly.DefinedTypes
where isClass ? type.IsSubclassOf( baseType ) :
isInterface ? type.ImplementedInterfaces.Contains( baseTypeInfo.AsType() ) : false
select type.AsType();
}

- 4,458
- 4
- 36
- 55