81

Is there a better (more performant or nicer code ;) way to find all derived Types of a Type? Currently im using something like:

  1. get all types in used Assemblies
  2. check my type with all those types if it is 'IsAssignable'

I was wondering if theres a better way todo this?

Yahoo Serious
  • 3,728
  • 1
  • 33
  • 37
Bluenuance
  • 4,813
  • 4
  • 23
  • 19

8 Answers8

99

I once used this Linq-method to get all types inheriting from a base type B:

    var listOfBs = (
                from domainAssembly in AppDomain.CurrentDomain.GetAssemblies()
                // alternative: from domainAssembly in domainAssembly.GetExportedTypes()
                from type in domainAssembly.GetTypes()
                where typeof(B).IsAssignableFrom(type)
                // alternative: && type != typeof(B)
                // alternative: && ! type.IsAbstract
                // alternative: where type.IsSubclassOf(typeof(B))
                select type).ToArray();

EDIT: As this still seems to get more rep (thus more views), let me add a fluent version and some more details:

   var listOfBs = AppDomain.CurrentDomain.GetAssemblies()
                // alternative: .GetExportedTypes()
                .SelectMany(domainAssembly => domainAssembly.GetTypes())
                .Where(type => typeof(B).IsAssignableFrom(type)
                // alternative: => type.IsSubclassOf(typeof(B))
                // alternative: && type != typeof(B)
                // alternative: && ! type.IsAbstract
                ).ToArray();

Details:

  • As the above-mentioned link states, this method uses Reflection on each call. So when using the method repeatedly for the same type, one could probably make it much more efficient by loading it once.
  • As Anton suggests, maybe you could (micro)optimize it using domainAssembly.GetExportedTypes() to retrieve only publicly visible types (if that's all you need).
  • As Noldorin mentions, Type.IsAssignable will also get the original (non-derived) type. (Type.IsSubclassOf will not, but Type.IsSubclassOf will not work if the base type is an interface). But of course, one can exclude the original base class: && type != typeof(B).
  • One may want/need to check for a concrete implemented class, i.e. ignore abstract classes: && ! assemblyType.IsAbstract. (Note that all interfaces are considered abstract, see MSDN.)
  • FWIW: as Jon Skeet suggested in a similar question: "it is trickier if you need to handle generics". A quick search returns some suggestions, but there are probably more, and I did not check them (e.g. for correctness):
Yahoo Serious
  • 3,728
  • 1
  • 33
  • 37
  • 2
    For some reason (I didn't dig into the details) `domainAssembly.GetExportedTypes()` can't be called from a dynamic assembly. (In my case, I created a class that extends the `Type` class with a `.GetDerivedTypes()` method, and I called it from an MVC Controller action.) – Bob.at.Indigo.Health May 27 '18 at 00:53
  • 1
    I had to switch the check around to get all types deriving from base type B, so: `where assemblyType.IsSubclassOf(typeof(B))` – tjeerdhans Jun 12 '18 at 09:57
13

I'm pretty sure the method you suggested is going to be the easier way to find all derived types. Parent classes don't store any information about what their sub-classes are (it would be quite silly if they did), which means there's no avoiding a search through all the types here.

Only recommendation is to use the Type.IsSubclassOf method instead of Type.IsAssignable in order to check whether a particular type is derived from another. Still, perhaps there is a reason you need to use Type.IsAssignable (it works with interfaces, for example).

Noldorin
  • 144,213
  • 56
  • 264
  • 302
7

The only optimization you can squeeze out of this is to use Assembly.GetExportedTypes() to retrieve only publicly visible types if that's the case. Other than that, there's no way to speed things up. LINQ may help with readability side of things, but not performance-wise.

You can do some short-circuiting to avoid unnecessary calls to IsAssignableFrom which is, according to Reflector, quite expensive one, by first testing whether the type in question is of required "class". That is, you're searching for classes only, there's no point in testing enums or arrays for "assignability".

Anton Gogolev
  • 113,561
  • 39
  • 200
  • 288
  • When using `GetExportedTypes()`, you'll need to filter out assemblies where `IsDynamic` is true, otherwise a `NotSupportedException` is thrown. I ran into this when starting an ASP.NET Core app; not sure if the dynamic assembly was created by ANC or a library I referenced. – Tobias J Feb 12 '19 at 21:44
4

Asuming baseType contains a System.Type object that you want to check against and matchType contains a System.Type object with the type of your current iteration (through a foreach-loop or whatever):

If you want to check wheather matchType is derived from the class represented by baseType I'd use

matchType.IsSubclassOf(baseType)

And if you want to check wheather matchType implements the interface represented by baseType I'd use

matchType.GetInterface(baseType.ToString(), false) != null

Of course I'd store baseType.ToString() as a global variable so I wouldn't need to call it all the time. And since you probably would need this in a context where you have a lot of types, you could also consider using the System.Threading.Tasks.Parallel.ForEach-Loop to iterate through all your types...

4

I think there is no better or direct way.

Better: Use IsSubclassOf instead of IsAssignable.

Dario
  • 48,658
  • 8
  • 97
  • 130
  • 2
    What makes you say that IsSubclassOf is "better"? It doesn't work with interfaces, and I can't see any immediate benefit... – Jon Skeet May 13 '09 at 12:54
  • 13
    Subclassing _implies_ subtyping. Subtyping _implies_ assignability. For example, I and I have no subclass or subtype relationship, but one is assignable to the other if I is covariant or contravariant in T. When considering the question of subtyping, IsSubClassOf has no false positives but has false negatives and IsAssignable has no false negatives but does have false positives. Clearly neither is correct, so which is "better" is a judgment call; do you prefer false negatives or false positives? – Eric Lippert May 13 '09 at 15:21
1

I ended up using the code that the top answer gave. Only thing is I wanted the code to be more readable so I wrote basically the same thing but like this instead:

var derived_types = new List<Type>();
foreach (var domain_assembly in AppDomain.CurrentDomain.GetAssemblies())
{
  var assembly_types = domain_assembly.GetTypes()
    .Where(type => type.IsSubclassOf(typeof(MyType)) && !type.IsAbstract);

  derived_types.AddRange(assembly_types);
}

In my case I used type.IsSubClassOf(MyType) because I only wanted derived types excluding the base class like mentioned above. I also needed the derived types not to be abstract (!type.IsAbstract) so I am also excluding derived abstract classes.

  • I just now noticed this answer/version, and I was wondering if your issue with readibility is about using fluent Linq, or if you really prefer the `foreach` and `AddRange`. Anyway, it inspired me to add a fluent Linq example without separate constructor, `foreach` and `AddRange`. – Yahoo Serious May 26 '22 at 09:30
1

If you're just interested in browsing, then .NET Reflector has the ability to do this. However, it isn't something that's really feasible. Would you want all types that are in the currently loaded assemblies? Assemblies referenced by the executing assembly? There are many different ways to obtain a list of Types, and writing something that would account for (and provide options for) would be a pretty big cost with relatively low benefit.

What are you trying to do? There's likely a better (or at least more efficient) way to accomplish it.

Adam Robinson
  • 182,639
  • 35
  • 285
  • 343
0

Just create a static dictionary of derived types on start and do lookup with that. Eg. public static Dictionay<Type, Type[]> DerivedTypes { get;set; } where Type is any type you want to include in the search and Type[] is a list of derived types. Fill the dictionary up when app starts and use it during the whole life of the app.

ADM-IT
  • 3,719
  • 1
  • 25
  • 26