Finding subclasses

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I'm hoping that someone here will be able to direct me to some litrature on
how to get a list of a classes subclasses. I.e. need the inherited class to
know about subclasses that are inherited form it.

I guess that I'll have to use reflection but I can't find any examples on
MSDN.

Thanks in advance,
Mark
 
MR said:
I'm hoping that someone here will be able to direct me to some litrature on
how to get a list of a classes subclasses. I.e. need the inherited class to
know about subclasses that are inherited form it.

I guess that I'll have to use reflection but I can't find any examples on
MSDN.

You can only look through assemblies to find all subclasses - if you
don't know all the assemblies that might contain subclasses, you can't
find them.

With that restriction, you can use Assembly.GetTypes, and then go
through each type and use Type.IsAssignableFrom to find out if the type
is derived (directly or indirectly) from your base type.
 
MR said:
I.e. need
the inherited class to know about subclasses that are inherited form
it.

You may want to look at your design here, this is generally not
considered a goal in inheritance, the point of being able to extend
objects is that the original object should not have to know anything
about it's descendant classes.

If you need to have a relationship with the descendant class and the
original class, you may be better off formally declaring what that
relationship is, and use some other mechanism (than reflection) to get
at it, perhaps a hashtable for instance or possibly a custom attribute,
personally I would favour a registration process into a hash table (or
some other list) A patterns book could help you with the design.

Cheers Tim.
 
Back
Top