Find all descendants of a type

Quite often for DI auto-wiring I need to find all classes that implement a certain interface or inherit from some base abstract class. And reflection for that is brilliant. But I can never remember how to find these types, so every time I need to look up my old code. So I’ll record that in this blog. Here you go:

var profiles = Assembly.GetExecutingAssembly()
    .GetTypes()
    .Where(x => x != typeof(Profile) && typeof(Profile).IsAssignableFrom(x))
    .ToList();

This will give a list of types that all inherit from class Profile, but actually excludes the Profile class itself.