By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,333 Members | 1,060 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,333 IT Pros & Developers. It's quick & easy.

implements interface

P: n/a
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
Nov 21 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
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.

<to********@hotmail.com> wrote in message
news:a9**************************@posting.google.c om...
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

Nov 21 '05 #2

P: n/a
<to********@hotmail.com> wrote in message
news:a9**************************@posting.google.c om...
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
Nov 21 '05 #3

P: n/a
to********@hotmail.com wrote:
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/)
Nov 21 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.