6

I've got an interface with type T objects like so

public interface IService<T>

I've got a bunch of these interfaces with all different type T's. How can I add these interfaces into a List of some sort, iterate through the list and call a common method used by the interface. Any ideas? Is there an alternative approach?

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

4

1 に答える 1

13

メソッドが一般的である場合、おそらく に依存しないTため、次のことができるはずです。

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

次に、 を持ち、次のようList<IService>にします。

foreach(var svc in list) svc.TheMethod();
于 2012-07-03T09:12:01.453 に答える