473,406 Members | 2,847 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

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

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
Nov 15 '05 #1
5 2533
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.

--
Justin Rogers
DigiTec Web Consultants, LLC.

"Val Savvateev" <vs********@meridium.com_NO_SPAM> wrote in message
news:%2***************@TK2MSFTNGP09.phx.gbl...
Well, there is a way to find out whether particular class was derived from a given class. Thus, for instance you could enumerate through all classes in
an assembly and tell whether they are derived from the base.

But by just having a base class you will never tell how many classes were
(are, will be) derived from it.

"Nathan Bullock" <na*****************@yahoo.ca> wrote in message
news:52**************************@posting.google.c om...
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


Nov 15 '05 #2
Justin Rogers <Ju****@games4dotnet.com> wrote:
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))
{
...
}
}

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet/
If replying to the group, please do not mail me too
Nov 15 '05 #3

"Jon Skeet" <sk***@pobox.com> wrote in message
news:MP************************@news.microsoft.com ...
Justin Rogers <Ju****@games4dotnet.com> wrote:
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


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(typ eof(short)));
}
}

<snip>

/Magnus Lidbom


Nov 15 '05 #4
Magnus Lidbom <ma***********@hotmail.com> wrote:
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 ;)


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(typ eof(short)));
}
}


Yes, fair point.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet/
If replying to the group, please do not mail me too
Nov 15 '05 #5
Thank You for your replies.

The solutions given worked perfectly.

Nathan Bullock
Nov 15 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

24
by: Shao Zhang | last post by:
Hi, I am not sure if the virtual keyword for the derived classes are required given that the base class already declares it virtual. class A { public: virtual ~A();
7
by: Tron Thomas | last post by:
Under the right compiler the following code: class Base { public: virtual void Method(int){} }; class Derived: public Base {
9
by: justanotherguy63 | last post by:
Hi, I am designing an application where to preserve the hierachy and for code substitability, I need to pass an array of derived class object in place of an array of base class object. Since I...
3
by: J.J. Feminella | last post by:
(Please disregard the previous message; I accidentally sent it before it was completed.) I have source code similar to the following. public class Vehicle { protected string dataV; // ......
2
by: Jessica | last post by:
I have a base class and a derived class, but I am getting errors when I try to access functions of the derived class. Simplified version of my code is as follows: //////////////// // test2.hh...
6
by: ivan.leben | last post by:
I want to write a Mesh class using half-edges. This class uses three other classes: Vertex, HalfEdge and Face. These classes should be linked properly in the process of building up the mesh by...
26
by: nyathancha | last post by:
Hi, How Do I create an instance of a derived class from an instance of a base class, essentially wrapping up an existing base class with some additional functionality. The reason I need this is...
9
by: desktop | last post by:
On this page: http://www.eptacom.net/pubblicazioni/pub_eng/mdisp.html Shape specify the virtual function: virtual double Intersect( const Shape& s) = 0; then the derived class Circle...
15
by: Bob Johnson | last post by:
I have a base class that must have a member variable populated by, and only by, derived classes. It appears that if I declare the variable as "internal protected" then the base class *can*...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.