vendredi 16 janvier 2015

Get classes from assembly that implement a specific interface

I have an interface such as this:



public interface IMyInterface<T>
{
void MyMethod(T obj);
}


I'm trying to find if an assembly has any classes that implement this. I found some examples but they are all demonstrate checking an implementation of a simple interface that don't have T.


Here is what I wrote:



var interfaceType = typeof(IMyInterface<>);
Assembly assembly = Assembly.LoadFrom(assemblyFile);
var allTypes = assembly.GetTypes();
foreach(Type type in allTypes)
{
var isImplementing = interfaceType.IsAssignableFrom(type);
}


I also tried doing this:



var interfaces = type.GetInterfaces();
var isImplementing = interfaces.Contains(interfaceType);


isImplementing is always false.






Aucun commentaire:

Enregistrer un commentaire