Iterate through a list of interfaces

I have an interface with types of T objects, for example:

public interface IService<T> 

I have a bunch of these interfaces with all the different types of T. How can I add these interfaces to a list of some kind, iterate over the list and call the general method used by the interface. Any ideas? Is there an alternative approach?

If I add it to the list, I just don't see a way to type T correctly.

+6
source share
1 answer

If the method is generic, then apparently it does not depend on T , so you should be able to:

 interface IService { void TheMethod(); } interface IService<T> : IService { // other stuff } 

then we have a List<IService> and simply:

 foreach(var svc in list) svc.TheMethod(); 
+13
source

Source: https://habr.com/ru/post/919562/


All Articles