mardi 12 décembre 2017

Get type that implements generic interface by searching for a specific generic interface parameter

I'd like to create a method that returns a type (or IEnumerable of types) that implement a specific interface that takes a type parameter -- however I want to search by that generic type parameter itself. This is easier demonstrated as an example:

Method signature that I want:

 public IEnumerable<Type> GetByInterfaceAndGeneric(Type interfaceWithParam, Type specificTypeParameter)

And then if I have the below objects

  public interface IRepository<T> { };
  public class FooRepo : IRepository<Foo> { };
  public class DifferentFooRepo : IRepository<Foo> {};

I then want to be able to do:

  var repos = GetByInterfaceAndGeneric(typeof(IRepository<>), typeof(Foo));

and get an IEunumerable containing the types FooRepo and DifferentFooRepo.

This is very similar to this question, however using that example I would like to search by both IRepository<> and by User.





Aucun commentaire:

Enregistrer un commentaire