mercredi 13 mai 2020

Retrieve unique list of classes that implement multiple of the same generic interface

Hope the title wasn't too confusing but here goes.

Say I have a generic interface

public interface IBase<T>{
}

Followed by classes that implement multiple of the interfaces (but with different generic) like so:

public class Base : IBase<TypeA>, IBase<TypeB>{
}

How would I retrieve list of classes that implemented the interface? My current solution :

from x in AppDomain.currentDomain.GetAssemblies()
   .SelectMany(s => s.GetTypes())
   from z in x.GetInterfaces()
   where
   z.IsGenericType &&
   basetype.IsassignableFrom(z.GetGenericTypeDefinition())
   select x

Issue with this is that it retrieves multiple of the same classes.

Any assistance will be appreciated!





Aucun commentaire:

Enregistrer un commentaire