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

杀马特。学长 韩版系。学妹 提交于 2019-12-10 21:00:06

问题


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!


回答1:


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.



来源:https://stackoverflow.com/questions/5398382/how-to-find-out-a-list-of-types-in-base-class-library-that-implement-specific-in

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!