Is there a way to find which .NET classes implement a particular interface?

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

+3
source share
5 answers

To find it on MSDN, I usually go to Google, type something like “MSDN IList” and get the IList interface , which has a section called “Classes that implement IList”. This is true for any of the interface classes.

If you find a base class, such as DictionaryBase , a link appears called Derived Classes, which takes you to a tree showing the inheritance hierarchy .

+3
source

I think this is possible using Reflector

+5
source

.

, ( mscorlib, , string), , :

.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);
        }
    }
}

implementors Types, IList. IList , IDictionary, ICollection ..

Edit:

AppDomain, :

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

, . , Reflector - , ( , ). , GAC ... , Reflector, , , .

+3

Do you have a specific use case? On top of my head you can 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)
0
source

Source: https://habr.com/ru/post/1712723/


All Articles