Is there any way to find all the derived classes of a base class?

N

Nathan Bullock

Hi,

I have a base class, say Base and there are two classes, say Class1
and Class2 which are derived from Base. Is there any way for me, say
from a static method in Base, to get a list of all classes derived
from Base?

class abstract Base
{
static public ArrayList AllDerived()
{
// Here I want to return a list containing typeof(Class1) and
typeof(Class2)
}
}

class Class1 : Base
{
public Class1() {}
}

class Class2 : Base
{
public Class1() {}
}

Thank you.

Nathan Bullock
 
J

Justin Rogers

The .NET QuickStart tutorials have an excellent ClassBrowser application
that encapsulates methods for finding inheritance chains and derived
classes. It is not difficult to find derived classes, just time consuming,
since as Val points out you have to iterate over all types and check their
inheritance chains to see if they derive from the target type.
 
J

Jon Skeet

Justin Rogers said:
The .NET QuickStart tutorials have an excellent ClassBrowser application
that encapsulates methods for finding inheritance chains and derived
classes. It is not difficult to find derived classes, just time consuming,
since as Val points out you have to iterate over all types and check their
inheritance chains to see if they derive from the target type.

You don't have to go through their inheritance chains manually - just
use Type.IsAssignableFrom - the BCL will do the work for you. The code
becomes as simple as:

foreach (Type type in assembly.GetTypes())
{
if (baseType.IsAssignableFrom(type))
{
...
}
}
 
M

Magnus Lidbom

Jon Skeet said:
You don't have to go through their inheritance chains manually - just
use Type.IsAssignableFrom

I'd use Type.IsSubclassOf in this context. Type.IsAssignableFrom would
do the right thing, except for claiming that the class inherits itself ;), but
is so unbelievably badly named it's almost guaranteed to cause confusion.

This simple program demonstrates why i feel Type.IsAssignableFrom is not just
badly named, but incorrectly named:

using System;
class Temp
{
[STAThread]
static void Main(string[] args)
{
int myInt = (short)1;
Console.WriteLine(typeof(int).IsAssignableFrom(typeof(short)));
}
}

<snip>

/Magnus Lidbom
 
J

Jon Skeet

Magnus Lidbom said:
I'd use Type.IsSubclassOf in this context. Type.IsAssignableFrom would
do the right thing, except for claiming that the class inherits itself ;)

True. On the other hand, it's definitely what you want to use if you
want to find implementations of interfaces.
but is so unbelievably badly named it's almost guaranteed to cause confusion.

I certainly have to look it up every time to make sure I get it the
right way round.
This simple program demonstrates why i feel Type.IsAssignableFrom is not just
badly named, but incorrectly named:

using System;
class Temp
{
[STAThread]
static void Main(string[] args)
{
int myInt = (short)1;
Console.WriteLine(typeof(int).IsAssignableFrom(typeof(short)));
}
}

Yes, fair point.
 
N

Nathan Bullock

Thank You for your replies.

The solutions given worked perfectly.

Nathan Bullock
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top