samedi 13 juin 2020

How to find all classes (types) that are implemented a nested interface in C# .NET Core?

Please consider the following sample:

    public interface IRepository<T> {} // NESTED

    public class Student {}
    public class Person  {}

    public interface IStudentRepository : IRepository<Student> {}
    public interface IPersonRepository : IRepository<Person> {}

    public class StudentRepo : IStudentRepository {}
    public class PersonRepo : IPersonRepository {}

I want to find all classes (StudentRepo and PersonRepo) that are implemented IRepository<T> in C# (.NET Core 3+).

When I use IStudentRepository or IPersonRepository to find types, everything is OK but does not work by searching typeof(IRepository<>)!

this block of code returns nothing

var repoTypes = AppDomain.CurrentDomain.GetAssemblies()
                .SelectMany(s => s.GetTypes())
                .Where(x => x.GetInterfaces().Containes(typeof(IRepository<>)))
                .ToList()
                ;

Can anyone help me?





Aucun commentaire:

Enregistrer un commentaire