mardi 17 avril 2018

Reflection, get types that implement an interface, but don't implement another one

I've got two types of repository classes in my API:

Those with both IRepository and IReportRepository:

internal class BuildingRepository : IBuildingRepository
public interface IBuildingRepository : IRepository<Building>, IReportRepository<BuildingReport>

And those with just IRepository:

internal class AppointmentRepository : IAppointmentRepository
public interface IAppointmentRepository : IRepository<Appointment>

How can I return all repositories that only implement IRepository and not IReportRepository. I thought it would be something like this:

var repoInterfaceType = typeof(IRepository<>);
var reportRepo = typeof(IReportRepository<>);
var repoTypes = asm.GetTypes().Where(x =>
            !x.IsInterface && !x.IsAbstract && x.IsClass && 
            !x.IsGenericType && x.GetInterfaces().Any(y =>
                y.IsGenericType &&
                repoInterfaceType.IsAssignableFrom(y.GetGenericTypeDefinition()) && 
      !reportRepo.IsAssignableFrom(y.GetGenericTypeDefinition()))).ToList();

But it is still returning me both. What am I missing?





Aucun commentaire:

Enregistrer un commentaire