Get all classes that implement a generic interface

8

I have an interface similar to this:

public interface InterfaceA<T>
{
   T Exemplo();
}

And other classes implement it.

public class ExemploA : InterfaceA<Int32>
{
    Int32 Exemplo();
}

public class ExemploB : InterfaceA<String>
{
    String Exemplo();
}

Searching I found this in SOen, but I can not get the type of the interface without generics .

var type = typeof(InterfaceA); //erro

Does anyone know how to get type of classes ExemploA , ExemploB , searching for InterfaceA , within a certain assembly ?

    
asked by anonymous 13.01.2017 / 17:33

1 answer

5

With to search for < in> name :

var types = System.Reflection.Assembly.GetExecutingAssembly().GetTypes()
                .Where(p => p.GetInterfaces().Where(c => c.Name.Contains("InterfaceA"))
                             .Any())                
                .ToList();

or

The generic search for Interface :

var types = System.Reflection.Assembly.GetExecutingAssembly().GetTypes()
                .Where(p => p.GetInterfaces()
                             .Where(c => c.IsGenericType &&
                                    c.GetGenericTypeDefinition() == typeof(InterfaceA<>))
                                     .Any())                
                .ToList();

13.01.2017 / 17:59