For Programmers: Free Programming Magazines  


Home > Archive > C# > April 2005 > Obtaining info about all classes which implement an interface









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Obtaining info about all classes which implement an interface
Anirudh

2005-04-07, 8:57 am

hi,
is there i can programatically know all the classes which derive
from a particular interface or class. i.e. is there any way of being
aware of all the derived classes of a known base class? please help.

Bruce Wood

2005-04-07, 8:57 pm

I believe that you have to search the types in each assembly. You can
get the list of assemblies in the current application domain like this:

Assemblies[] domainAssemblies= AppDomain.CurrentDomain.GetAssemblies();
foreach (Assembly a in domainAssemblies)
{
Type[] assemblyTypes = a.GetExportedTypes();
foreach (Type t in assemblyTypes)
{
if (t.IsSubclassOf(searchClassType))
{
... do something, like add to a result list ...
}
}
}

Anirudh

2005-04-08, 8:58 am

thank you so much, bruce.

Bruce Wood

2005-04-11, 8:58 pm

I believe that you have to search the types in each assembly. You can
get the list of assemblies in the current application domain like this:

Assemblies[] domainAssemblies= AppDomain.CurrentDomain.GetAssemblies();
foreach (Assembly a in domainAssemblies)
{
Type[] assemblyTypes = a.GetExportedTypes();
foreach (Type t in assemblyTypes)
{
if (t.IsSubclassOf(searchClassType))
{
... do something, like add to a result list ...
}
}
}

David Alpert

2005-04-11, 8:58 pm

I believe you can also use:

[same code as Bruce Wood's, but in the innermost 'if' statement...]

if (t is ISomeInterface)
{
// do something
}

"Bruce Wood" <brucewood@canada.com> wrote in message news:<1112900123.011263.311580@f14g2000cwb.googlegroups.com>...
> Assemblies[] domainAssemblies= AppDomain.CurrentDomain.GetAssemblies();
> foreach (Assembly a in domainAssemblies)
> {
> Type[] assemblyTypes = a.GetExportedTypes();
> foreach (Type t in assemblyTypes)
> {
> if (t.IsSubclassOf(searchClassType))
> {
> ... do something, like add to a result list ...
> }
> }
> }

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2009 codecomments.com