Suppose we have the following services:
interface IService { }
interface IService<T> : IService {
T Get();
}
In ASP.Net-Core, after we have registered some implementations with different T
we can get all registered services like this:
IEnumerable<IService> services = serviceProvider.GetServices<IService>();
Now, because I need access to the generic type parameter from the other interface that is not an option. How can I retrieve all implementations of IService<T>
without losing the generic type? Something like:
IEnumerable<IService<T>> services = serviceProvider.GetServices<IService<T>>();
foreach (var s in services) {
Method(s);
}
// Here we have a generic method I don't have control over.
// I want to call the method for each `T` registered in DI
void Method<T>(IService<T> service) {
Type t = typeof(T); // This here will resolve to the actual type, different in each call. Not object or whatever less derived.
}
And all of this should have a somewhat decent performance.