Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get all derived types of a type

Tags:

c#

.net

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?

like image 577
Bluenuance Avatar asked May 13 '09 12:05

Bluenuance


2 Answers

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 assemblyType in domainAssembly.GetTypes()                     where typeof(B).IsAssignableFrom(assemblyType)                     // alternative: where assemblyType.IsSubclassOf(typeof(B))                     // alternative: && ! assemblyType.IsAbstract                     select assemblyType).ToArray(); 

EDIT: As this still seems to get more rep (thus more views), let me add some more 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).
  • One may want/need to check for a 'real' class: && ! assemblyType.IsAbstract. (Note that all interfaces are considered abstract, see MSDN.)
like image 200
Yahoo Serious Avatar answered Sep 22 '22 07:09

Yahoo Serious


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).

like image 39
Noldorin Avatar answered Sep 19 '22 07:09

Noldorin