473,326 Members | 2,255 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,326 software developers and data experts.

Determining names of derived classes at runtime

All:

Is it possible at runtime to determine the names of all classes derived
from a given class?
--
John Hardin KA7OHZ <jo***@aproposretail.com>
Internal Systems Administrator voice: (425) 672-1304
Apropos Retail Management Systems, Inc. fax: (425) 672-0192
-----------------------------------------------------------------------
...the Fates notice those who buy chainsaws...
-- www.darwinawards.com
Nov 16 '05 #1
5 2646
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

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Nov 16 '05 #2
On Fri, 18 Jun 2004 02:41:12 +0200, Mattias Sjögren wrote:
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)?


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 KA7OHZ <jo***@aproposretail.com>
Internal Systems Administrator voice: (425) 672-1304
Apropos Retail Management Systems, Inc. fax: (425) 672-0192
-----------------------------------------------------------------------
...the Fates notice those who buy chainsaws...
-- www.darwinawards.com
Nov 16 '05 #3
John Hardin <jo***@aproposretail.com> wrote:
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.


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:sk***@pobox.com
you'll find some sample code I wrote a while ago.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #4
>
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/Cshar...cle.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.

Nov 16 '05 #5
Adrian Mascarenhas (MS) <ad******@online.microsoft.com> wrote:
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.


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.

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

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

Similar topics

4
by: Clint Hill | last post by:
I am working on a project that I would like to have some extensibility for later. So with this I am using interfaces to pass objects to derived classes. However I am running into a situation where...
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...
12
by: Steve Teeples | last post by:
Can someone tell me the correct method of casting and object at run time. Here is a sample of what I'm trying to do. classA acts as a base for classes that execute code in classes derived from...
7
by: Jerry Nixon | last post by:
I hope this is easy, First, I simply must not override MyMethod as shown in the sample code - that is an unbreakable requirement. The purpose of the event is to allow me to extend MyMethod...
8
by: Manuel | last post by:
Hi! If I've a vector filled with abstract classes, can I push in it the derived classes too? Even if derived classes have new methods? I've done some experiments, and it seem I can push the...
15
by: Jeff Mason | last post by:
Hi, I'm having a reflection brain fog here, perhaps someone can set me on the right track. I'd like to define a custom attribute to be used in a class hierarchy. What I want to do is to...
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*...
5
by: Joseph Barillari | last post by:
Hi python-list, I've just started using new-style classes and am a bit confused as to why I can't seem to alter methods with special names (__call__, etc.) of new-style class instances. In other...
9
by: fgh.vbn.rty | last post by:
Say I have a base class B and four derived classes d1, d2, d3, d4. I have three functions fx, fy, fz such that: fx should only be called by d1, d2 fy should only be called by d2, d3 fz should...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.