473,698 Members | 2,602 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2568
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********@mer idium.com_NO_SP AM> wrote in message
news:%2******** *******@TK2MSFT NGP09.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.goo gle.com...
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****@games4d otnet.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.IsAssignab leFrom - the BCL will do the work for you. The code
becomes as simple as:

foreach (Type type in assembly.GetTyp es())
{
if (baseType.IsAss ignableFrom(typ e))
{
...
}
}

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

"Jon Skeet" <sk***@pobox.co m> wrote in message
news:MP******** *************** *@news.microsof t.com...
Justin Rogers <Ju****@games4d otnet.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.IsAssignab leFrom


I'd use Type.IsSubclass Of in this context. Type.IsAssignab leFrom 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.IsAssignab leFrom is not just
badly named, but incorrectly named:

using System;
class Temp
{
[STAThread]
static void Main(string[] args)
{
int myInt = (short)1;
Console.WriteLi ne(typeof(int). IsAssignableFro m(typeof(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.IsAssignab leFrom
I'd use Type.IsSubclass Of in this context. Type.IsAssignab leFrom 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.IsAssignab leFrom is not just
badly named, but incorrectly named:

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


Yes, fair point.

--
Jon Skeet - <sk***@pobox.co m>
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
3296
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
8664
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
4798
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 am using vector class(STL), the compiler does not allow me to do this. I do realize there is a pitfall in this approach(size of arrays not matching etc), but I wonder how to get around this problem. I have a class hierachy with abstract base...
3
3490
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; // ... more protected fields }
2
2636
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 class BaseClass {
6
2940
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 calling Mesh class functions. Let's say they point to each other like this: class Vertex { HalfEdge *edge; }; class HalfEdge { Vertex* vert;
26
5363
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 because I am not always able to control/create all the different constructors the base class has. My problem can be described in code as follows ... /* This is the base class with a whole heap of constructors/functionality*/ public class Animal
9
2042
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 also specify:
15
3842
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* populate the variable, but the population is not *required* by the derived class (which must be the case). What would meet the requirements is if I create an abstract method in the base class that populates the member variable. In this case the...
0
9169
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9030
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8871
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7738
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6528
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5861
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4371
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4622
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3052
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.