tags:

views:

444

answers:

5

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?

+5  A: 

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
+1  A: 

Not that I know of. However, I don't think the important question is how to do it, but why? What are you trying to achieve? There's probably a better way to go about it.

AndrewS
I would love to explain, but unfortunately not something I can tell in a short comment :(Let's say its about analyzing assemblies from unknown managed programs for short.
Calamitous
+3  A: 

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
Note: IsSubclassOf does not work if the "base class" is actually an interface, while IsAssignableFrom does.
Stefan Steinegger
@Stefan: Yeah, I've mentioned that in my answer. It all depends on the exact context, which the asker wants to use.
Noldorin
+1  A: 

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
+1  A: 

I think there is no better or direct way.

Better: Use IsSubclassOf instead of IsAssignable.

Dario
What makes you say that IsSubclassOf is "better"? It doesn't work with interfaces, and I can't see any immediate benefit...
Jon Skeet
Subclassing _implies_ subtyping. Subtyping _implies_ assignability. For example, I<object> and I<string> have no subclass or subtype relationship, but one is assignable to the other if I<T> 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