implements interface

T

topdawg147

Is there any way to retrieve a list of objects that implement an
interface. For example, suppose we have the following:

Public Interface ISomething
Function SomeFunction() As String
End Interface

Public Class Foo1
Implements ISomething
...
End Class

Public Class Foo2
Implements ISomething
...
End Class

I would like to retrieve a list of all classes that implement the
ISomething interface (Foo1, Foo2)--not the opposite (retrieving a list
of interfaces). Any thoughts? Is this possible using reflection?

Thanks,
Todd
 
I

Imran Koradia

Todd,

I don't think this is possible without doing an actual search on the objects
and retrieving the metadata from the object using reflection. Think about
it: Its easy to retrieve all the interfaces implemented by an object since
we're only looking at one object and .NET has a mechanism of storing and
retrieving metadata of an object. However, if we need to find all the
objects that implement a particular interface, where would we be looking?
Its not like .NET stores metadata with an Interface about the objects that
are implementing it - which would be impossible to do as far as I can tell.
IMHO, you need to define the scope of your search first - such as search
objects within the currently executing assembly, or search objects within
given assemblies, etc which implement a given interface. You'll then need to
load up the assemblies (ofcourse not the currently executing one) and
retrieve the object information from them using Reflection and then
determine whether each one implements the interface in question.

I hope this helps..
Imran.
 
J

Jeremy

Is there any way to retrieve a list of objects that implement an
interface. For example, suppose we have the following:

Sure, it's actually very easy. This implementation searches the current
assembly for all objects which implement the ICollection Interface:

-----8<-------------------
Dim iface As Type
Dim targetInterface as Type = GetType(ICollection)

For Each thisType As Type In
Reflection.Assembly.GetExecutingAssembly.GetTypes
iface = thisType.GetInterface(targetInterface.Name)
If Not iface Is Nothing Then Console.WriteLine("Found interface: " &
thisType.Name)
Next
-----8<-------------------

Of course, there are more sofisticated ways of doing this (see
Type.FindInterfaces( )), but this is the easiest.

HTH,
Jeremy
 
C

Codo

Is there any way to retrieve a list of objects that implement an
interface. For example, suppose we have the following:

Public Interface ISomething
Function SomeFunction() As String
End Interface

Public Class Foo1
Implements ISomething
...
End Class

Public Class Foo2
Implements ISomething
...
End Class

I would like to retrieve a list of all classes that implement the
ISomething interface (Foo1, Foo2)--not the opposite (retrieving a list
of interfaces). Any thoughts? Is this possible using reflection?

Thanks,
Todd

What you need is a tool called Reflector
(http://www.aisto.com/roeder/dotnet/)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top