Determining names of derived classes at runtime

  • Thread starter Thread starter John Hardin
  • Start date Start date
J

John Hardin

All:

Is it possible at runtime to determine the names of all classes derived
from a given class?
 
John,
Is it possible at runtime to determine the names of all classes derived
from a given class?

The answer depends on where you expect to find these derived classes
(in the same assembly, in other loaded assemblies or in any assembly
on the system)?



Mattias
 
John,


The answer depends on where you expect to find these derived classes
(in the same assembly, in other loaded assemblies or in any assembly
on the system)?

Ideally I'd like to not have to worry about which assembly the derived
class is in; probably as a practical limit, all the assemblies loaded for
the current application.

I've continued to work on this and think it is possible. I'm continuing to
read up on the reflection classes and methods.

Thanks!
 
John Hardin said:
Ideally I'd like to not have to worry about which assembly the derived
class is in; probably as a practical limit, all the assemblies loaded for
the current application.

I've continued to work on this and think it is possible. I'm continuing to
read up on the reflection classes and methods.

You can find all the referenced assemblies of a given assembly, and
load any ones which haven't been already loaded, then recurse, etc.
It's not particularly pretty, but it's doable.

If you search on groups.google.com for
insubject:"dependency walker" author:[email protected]
you'll find some sample code I wrote a while ago.
 
Is it possible at runtime to determine the names of all classes derived
from a given class?

You can use System.Reflection to do this by getting the Type of the current class and then searching all types in the assembly and checking to see if their BaseType matches this type.

Here is a link on introduction to reflection:
http://www.codeguru.com/Csharp/Csharp/cs_misc/reflection/article.php/c4257/


--
Adrian Mascarenhas, Developer Division

This posting is provided "AS IS" with no warranties, and confers no rights.

Note: For the benefit of the community-at-large, all responses to this message are best directed to the newsgroup/thread from which they originated.
 
Adrian Mascarenhas (MS) said:
You can use System.Reflection to do this by getting the Type of the
current class and then searching all types in the assembly and
checking to see if their BaseType matches this type.

Usually, a better way of checking whether one type is derived from
another or implements a given interface is to use
Type.IsAssignableFrom. This means you can check for "grandchildren"
types as well without checking the base type of the base type, etc.
 
Back
Top