473,394 Members | 1,693 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,394 software developers and data experts.

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 4703
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.GetReferencedAssemblies.

You can find all the types within a type with Assembly.GetTypes, and
you can check type relations with Type.IsSubclassOf.

--
Jon Skeet - <sk***@pobox.com>
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.comwrote in message
news:MP************************@msnews.microsoft.c om...
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.GetReferencedAssemblies.
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.GetReferencedAssemblies? If I write that in assembly A
will that not just get the assemblies referenced from A? Or will
Assembly.GetReferencedAssemblies 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.GetReferencedAssemblies.

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.GetReferencedAssemblies? If I write that in assembly A
will that not just get the assemblies referenced from A? Or will
Assembly.GetReferencedAssemblies 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.GetExecutingAssembly()

--
Jon Skeet - <sk***@pobox.com>
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.GetReferencedAssemblies() called in assembly
A will not find assembly B.

The solution is using Assembly.GetEntryAssembly() method to get the first
root assembly(Exe) reference, and then recursively loop through all its
referenced assemblies Assembly.GetReferencedAssemblies(). 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**************@TK2MSFTNGP03.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.microsoft.comwrote in message
news:dL**************@TK2MSFTNGHUB02.phx.gbl...
Yes, in this scenario, there is no direct reference relationship between
assembly A and B, so Assembly.GetReferencedAssemblies() called in assembly
A will not find assembly B.

The solution is using Assembly.GetEntryAssembly() method to get the first
root assembly(Exe) reference, and then recursively loop through all its
referenced assemblies Assembly.GetReferencedAssemblies(). 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.IsAssignableFrom(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 IsAssignableFrom 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
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>...
0
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...
16
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...
4
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...
4
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...
7
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'. ...
1
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...
2
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...
7
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
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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?
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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
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...

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.