Search code examples
.netbase-class-library

How to find out a list of types in Base Class Library that implement specific interface?


Sometimes I want to find out a list of all standard .NET types that implement a specific interface. Usually it is out of curiosity, sometimes there is also some practical purpose (but that's not the point).

I tried to get this out of MSDN, but type's page only contains links children of types, not types implementing interface.

Do you know any trick how to do this (or a tool that would help)?

I wrote this code (ICollection is the type being investigated):

        var result =
            from assembly in AppDomain.CurrentDomain.GetAssemblies().ToList()
            from type in assembly.GetTypes()
            where typeof(ICollection).IsAssignableFrom(type)
            select type;

        foreach (var type in result)
        {
            Console.Out.WriteLine(type.FullName);
        }

But this has some limitations:

  1. It only searches currently loaded assemblies.
  2. I couldn't figure out a way to do this for generic interfaces (ICollection<> wouldn't work).
  3. It would be nice if it provided links to MSDN (but I gues that could be fixed).

Thanks for help!


Solution

  • It only searches currently loaded assemblies.

    There's always the "Add Reference" dialog, but you might want to look at the question: List all available .NET assemblies.

    I couldn't figure out a way to do this for generic interfaces (ICollection<> wouldn't work)

    Try this query instead:

    from assembly in AppDomain.CurrentDomain.GetAssemblies()
    from type in assembly.GetTypes()
    where type.GetInterfaces()
              .Any(i => i.IsGenericType
                     && i.GetGenericTypeDefinition() == typeof(ICollection<>))
    select type;
    

    It would be nice if it provided links to MSDN.

    .NET Reflector supports searching for types implementing an interface (choose "Derived Types" under a type) as well as searching MSDN for documentation on a type (right-click on a type and choose "Search MSDN").

    If you don't like that option, you could of course try to write something that runs a web-search on MSDN with the type's full-qualified name. I'm unaware if there's any metadata around that maps a type to its MSDN page or a clean way of accomplishing that.