473,769 Members | 1,748 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Finding all child classes.

I would like to use reflection to find all classes that inherit from my
current class, even if they are in another assembly I want to find them if
the current project has a reference to that assembly.
The reason for this is that I want to check some attributes of all child
classes and have the base class react to this information in the child
classes.

Is this possible in an easy way, or do I have to traverse all loaded
assemblies and all their classes to find the child classes?

Kind Regards,
Allan Ebdrup
Feb 28 '07 #1
9 4725
Allan Ebdrup <eb****@noemail .noemailwrote:
I would like to use reflection to find all classes that inherit from my
current class, even if they are in another assembly I want to find them if
the current project has a reference to that assembly.
The reason for this is that I want to check some attributes of all child
classes and have the base class react to this information in the child
classes.

Is this possible in an easy way, or do I have to traverse all loaded
assemblies and all their classes to find the child classes?
You can find all referenced assemblies (recursively) with
Assembly.GetRef erencedAssembli es.

You can find all the types within a type with Assembly.GetTyp es, and
you can check type relations with Type.IsSubclass Of.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Feb 28 '07 #2

"Jon Skeet [C# MVP]" <sk***@pobox.co mwrote in message
news:MP******** *************** *@msnews.micros oft.com...
Allan Ebdrup <eb****@noemail .noemailwrote:
>I would like to use reflection to find all classes that inherit from my
current class, even if they are in another assembly I want to find them
if
the current project has a reference to that assembly.
The reason for this is that I want to check some attributes of all child
classes and have the base class react to this information in the child
classes.

Is this possible in an easy way, or do I have to traverse all loaded
assemblies and all their classes to find the child classes?

You can find all referenced assemblies (recursively) with
Assembly.GetRef erencedAssembli es.
Say my base class is in assembly A and I have a child class in assembly B
Now I have a project that references A and B, how do I write code in my base
class so that it can see that there is a child class in Assembly B
Is it just Assembly.GetRef erencedAssembli es? If I write that in assembly A
will that not just get the assemblies referenced from A? Or will
Assembly.GetRef erencedAssembli es be evaluated at runtime to get all
referenced assemblies for the current running assembly?

Kind Regards,
Allan Ebdrup
Feb 28 '07 #3
Allan Ebdrup <eb****@noemail .noemailwrote:
You can find all referenced assemblies (recursively) with
Assembly.GetRef erencedAssembli es.

Say my base class is in assembly A and I have a child class in assembly B
Now I have a project that references A and B, how do I write code in my base
class so that it can see that there is a child class in Assembly B
Is it just Assembly.GetRef erencedAssembli es? If I write that in assembly A
will that not just get the assemblies referenced from A? Or will
Assembly.GetRef erencedAssembli es be evaluated at runtime to get all
referenced assemblies for the current running assembly?
You'd have to call it on assembly C (the assembly that references A and
B). You can get the currently executing assembly with

Assembly.GetExe cutingAssembly( )

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Feb 28 '07 #4
"Allan Ebdrup" <eb****@noemail .noemaila écrit dans le message de news:
eF************* **@TK2MSFTNGP04 .phx.gbl...

|I would like to use reflection to find all classes that inherit from my
| current class, even if they are in another assembly I want to find them if
| the current project has a reference to that assembly.
| The reason for this is that I want to check some attributes of all child
| classes and have the base class react to this information in the child
| classes.

This really isn't necessary; this is the kind of thing that virtual methods
are there for.

public class BaseClass
{
private void CheckAttributes ()
{
// setup parameters to pass to virtual method
HandleAttribute (/* params passed in*/);
// examine params and react
}

protected virtual void HandleAttribute (/*params from base class*/)
{
// ...
}
}

class DerivedClass
{
protected override void HandleAttribute (/*params from base class*/)
{
// ...
}
}

Joanna

--
Joanna Carter [TeamB]
Consultant Software Engineer
Feb 28 '07 #5
Hi Allan,

Thanks for your feedback.

Yes, in this scenario, there is no direct reference relationship between
assembly A and B, so Assembly.GetRef erencedAssembli es() called in assembly
A will not find assembly B.

The solution is using Assembly.GetEnt ryAssembly() method to get the first
root assembly(Exe) reference, and then recursively loop through all its
referenced assemblies Assembly.GetRef erencedAssembli es(). Note1 : the
assembly A and B may not be directly referenced by the Exe, so you may need
to recursively loop through all the referenced assemblies again.
Note2 : during the recursively loop, you should take care of the redundant
assemblies checking, so that once an assembly is checked, it will not be
checked in the further loop and search.

Additionally, I am not sure your problem context. Have you considered to
use virtual function and polymorphism to resolve this problem as Joanna
pointed out? This may be more suitable solution.

If you have anything unclear or need any help, please feel free to
feedback. Thanks.

Best regards,
Jeffrey Tan
Microsoft Online Community Support
=============== =============== =============== =====
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscripti...ult.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscripti...t/default.aspx.
=============== =============== =============== =====
This posting is provided "AS IS" with no warranties, and confers no rights.

Mar 1 '07 #6

"Joanna Carter [TeamB]" <jo****@not.for .spamwrote in message
news:ux******** ******@TK2MSFTN GP03.phx.gbl...
"Allan Ebdrup" <eb****@noemail .noemaila écrit dans le message de news:
eF************* **@TK2MSFTNGP04 .phx.gbl...

|I would like to use reflection to find all classes that inherit from my
| current class, even if they are in another assembly I want to find them
if
| the current project has a reference to that assembly.
| The reason for this is that I want to check some attributes of all child
| classes and have the base class react to this information in the child
| classes.

This really isn't necessary; this is the kind of thing that virtual
methods
are there for.

public class BaseClass
{
private void CheckAttributes ()
{
// setup parameters to pass to virtual method
HandleAttribute (/* params passed in*/);
// examine params and react
}

protected virtual void HandleAttribute (/*params from base class*/)
{
// ...
}
}

class DerivedClass
{
protected override void HandleAttribute (/*params from base class*/)
{
// ...
}
}

Joanna
Hi Joanna
The code i'm implementing is a attribute driven Object Relational Manager,
Based on the attributes I want to create objectes that correspond to some
type value stored in the database. I want to implement this functionality
only once in the base class but I want it to be able to construct the right
child class based on the type value stored in the database, therfore the
base class needs access to all it's child classes.
It's somewhat advanced and makes heavy use of reflection, but a virtual
function is not an option.

A bit more detail:

Say I have class A that has a constuctor that takes an id (int) that
correspond to a primary key in the database
In class B I have the following

public class B : Databinder<int>
{
[BindObject]
A _a
}

Suppose I have an instance og the B class in the variable b.
Now my databinder will save the _a objects id in the database when I persist
b whit the record for b
When I load b again my databinder constructs an instance of the A class and
puts it in _a automatically.

Now suppose I have a class C that inherits from A that I put an instance of
it in _a and save b again. When I load b again I want it to constunct an
instance of C not A, therefore I need a mechanism to construct the right
child class, this is where the need to find all child classes comes in,
because in the attributes of C I can see when to construct a C instance. I
could put this information in the base class but that wouldn't be very
composable, I don't want to have to change the base class every time I
inherit form it.

The databinder can do a lot of things and would take a lot of time to
explain in detail, but makes it so that I hardly have to write any SQL
code, it's all generated for me automatically. All I have to do is add
attributes to my class, and all the loading and persisting of objects it
there.

Kind Regards,
Allan Ebdrup
Mar 1 '07 #7
""Jeffrey Tan[MSFT]"" <je***@online.m icrosoft.comwro te in message
news:dL******** ******@TK2MSFTN GHUB02.phx.gbl. ..
Yes, in this scenario, there is no direct reference relationship between
assembly A and B, so Assembly.GetRef erencedAssembli es() called in assembly
A will not find assembly B.

The solution is using Assembly.GetEnt ryAssembly() method to get the first
root assembly(Exe) reference, and then recursively loop through all its
referenced assemblies Assembly.GetRef erencedAssembli es(). Note1 : the
assembly A and B may not be directly referenced by the Exe, so you may
need
to recursively loop through all the referenced assemblies again.
Note2 : during the recursively loop, you should take care of the
redundant
assemblies checking, so that once an assembly is checked, it will not be
checked in the further loop and search.
Thanks for the pointers I will do that.

I found a recommendation to use:
type.IsAssignab leFrom(myType)
To see if a specific type is a child class og myType, would you agree that
this is the best way to do it?
Additionally, I am not sure your problem context. Have you considered to
use virtual function and polymorphism to resolve this problem as Joanna
pointed out? This may be more suitable solution.
Se my answer to Joanna why a virtual function is not an option.

Kind Regards,
Allan Ebdrup.
Mar 1 '07 #8
Hi Allan ,

The difference between IsAssignableFro m and IsSubclassOf normally comes
from the interface area. IsSubclassOf method can not be used to determine
if an interface is and child interface of another interface. If you are
only dealing with class types without interface, IsSubclassOf is enough for
you, see the blog below:
"Using IsSubclassOf on an Interface type"
http://dotnetjunkies.com/WebLog/john...21/134854.aspx

Also, thank you for sharing the detailed context information regarding your
application.

Anyway, if you still need any help, please feel free to tell me, thanks.

Hope this helps.

Best regards,
Jeffrey Tan
Microsoft Online Community Support
=============== =============== =============== =====
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscripti...ult.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscripti...t/default.aspx.
=============== =============== =============== =====
This posting is provided "AS IS" with no warranties, and confers no rights.

Mar 2 '07 #9
Hi Allan ,

Have you reviewed my last reply to you? Does it make sense to you? If you
still need any help or have any concern, please feel free to feedback,
thanks.

Best regards,
Jeffrey Tan
Microsoft Online Community Support
=============== =============== =============== =====
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscripti...ult.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscripti...t/default.aspx.
=============== =============== =============== =====
This posting is provided "AS IS" with no warranties, and confers no rights.

Mar 7 '07 #10

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

Similar topics

3
1742
by: Jamie Green | last post by:
Using MSXML3.0, with the Dom SelectionLanguage set to Xpath I am trying to query the following document <Root> <Child>Name</Child> <Child>John</Child> <Child>Smith</Child> <Child>23</Child> <Child>Name</Child> <Child>Peter</Child>
0
1385
by: CoolPint | last post by:
I am trying to write a generic heapsort (of course as a self-exercise) with Iterator interface: something like blow.... But I got into trouble finding out the Iterator to the Child node. If indexing was used, I could do something like child = hole * 2 + 1; but since only thing the function accepts are random access Iterators, how do I calculate the Iterator to the child node? template <typename Iterator, typename Functor> void...
16
2675
by: Suzanne Vogel | last post by:
Hi, I've been trying to write a function to test whether one class is derived from another class. I am given only id's of the two classes. Therefore, direct use of template methods is not an option. Let's call the id of a class "cid" (for "class id"). The function signature should look like this: ******************************************
4
9783
by: james | last post by:
I have a custom UserControl, which can have many sub class levels derived from it. I want to be able to discover all the components at Load time, but the only components I can see from the base class are the private components internal to the base class itself. What I want are ALL components for the entire class no matter how many levels of sub-classing this particular control contains. I do not want to have to force the child classes to...
4
4570
by: Danny Tuppeny | last post by:
Hi all, I've been trying to write some classes, so when I have a parent-child relationship, such as with Folders in my application, I don't have to remember to add a parent reference, as well as adding to the child collection, eg: parent.Children.Add(child); child.Parent = parent;
7
2039
by: msxkim | last post by:
How to execute functions in the parent class first and then functions in the child class? For example, I have a parent class with functions 'ONE' and 'TWO' and child class has a function 'THREE'. How should I declare classes so that all three functions are executed when child class is called? Class Parent public function ONE 'code end fuction
1
6535
by: =?Utf-8?B?cmFuZHkxMjAw?= | last post by:
The code below is pretty simple. Calling Talker() in the parent returns "Parent", and calling Talker() in the child returns "Child". I'm wondering how I can modify the code so that a call to the Talker() in Parent will call the Talker() method in every child class. The kicker is that I have many different Child classes, and not all Child classes will be loaded when Talker() in the Parent is called. Thanks, Randy
2
1660
by: skrebbel | last post by:
Is there any faster/easier way of finding a child node of a certain DOMNode object than doing something like function getChildByTagname($curnode, $name) { foreach($curnode->childNodes as $node) { if($node->nodeName==$name) { return $node;
7
1695
by: joproulx | last post by:
Hi, I was wondering if there was a way with Reflection to find dynamically if an object was referencing indirectly another object. A simple example would be: Object1 | --Object2 |
0
9589
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10047
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...
1
9995
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9863
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
8872
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
7410
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
5304
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
5447
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3962
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.