views:

118

answers:

5

For example if I wanted to see what my .NET options were for something implementing IList or IDictionary. Is there a way to find that for example in the MSDN documentation?

+1  A: 

You can lookup the interfaces a certain type implements with this method:

http://msdn.microsoft.com/en-us/library/system.type.getinterfaces.aspx

Should do the trick

Henri
I think the question was asking for the opposite: all classes that implement a certain interface.
Don Kirkby
I need the other way around, kind of.
Svish
+5  A: 

I think it's possible using Reflector

Sorantis
+1 - Reflector is a great tool.
Achilles
Search for the interface in Reflector, and then expand the Derived Types under that interface.
Don Kirkby
+1 I can verify that it does indeed work with Reflector
Fredrik Mörk
+1 I love reflector
Janie
Make sure you tell Reflector about all the assemblies that you are using. Otherwise, it won't be able to find those implementations.
Daniel Yankowsky
+3  A: 

To find it in MSDN, I usually go to Google, type something like "MSDN IList", and to get to IList Interface which has a section "Classes that Implement IList". That's true for any of the interface classes.

If you find a base class such as DictionaryBase, there will be a link called Derived Classes which will take you to a tree showing the inheritance hierarchy.

lavinio
A: 

Do you have a specific use case? Off the top of my head you could use:

System.Collections.ArrayList (or derived)
System.Collections.ObjectModel.Collection<T> derived
System.Collections.CollectionBase derived
System.Collections.DictionaryBase derived
System.Collections.Hashtable (or derived)
280Z28
+2  A: 

You can also do this programmatically.

If you know the assembly where the types are located (I'm using mscorlib as that is where string is located) you can build a list using this method:

.Net 3.0

List<Type> implementors = 
   Assembly.GetAssembly(typeof(string))
    .GetTypes()
    .Where(type => type.GetInterfaces().Contains(typeof(IList)))
    .ToList();

.Net 2.0

List<Type> implementors = new List<Type>();

foreach (Type type in Assembly.GetAssembly(typeof(string)).GetTypes())
{
    foreach (Type interfaceType in type.GetInterfaces())
    {
     if (interfaceType == typeof(IList))
     {
      implementors.Add(type);
     }
    }
}

The implementors list will hold a list of of Types that implement the interface IList. You can change IList to any interface you'd like, IDictionary, ICollection, etc.

Edit:

If you want to extend this to all assemblies in the current AppDomain, you could do:

List<Type> implementors = 
AppDomain.CurrentDomain.GetAssemblies()
.SelectMany(assembly => assembly.GetTypes()
                        .Where(type => type.GetInterfaces().Contains(typeof(IList)))
            ).ToList();

It really all depends on what you're doing with the data. If you just want to view them for your own personal satisfaction, Reflector is going to be your easiest option - especially if you want to look beyond assemblies loaded in your application domain (assuming you have an application to begin with). I suppose you could load all the assemblies from the GAC in that case... but this is basically what Reflector does except you get to pick and choose which ones you want individually.

John Rasch
Would be more difficult if having to take other assemblies into account as well... or are there a way of scanning through all assemblies that are considered part of the .Net Framework?
Svish