如果有一组类都实现了一个接口(interface)。
interface IMyinterface<T>
{
int foo(T Bar);
}
我想把它们全部塞进一个列表中并列举它们。
List<IMyinterface> list
foreach(IMyinterface in list)
// etc...
但编译器想知道 T 是什么。我可以这样做吗?我该如何克服这个问题?
最佳答案
没有 IMyinterface 类型,只有 IMyinterface`1 类型需要类型参数。您可以创建一个 IMyinterface 类型:-
interface IMyinterface { ... }
然后继承它
interface IMyinterface<T> : IMyinterface { ... }
您需要将要在 foreach 循环中使用的任何成员移动到 IMyinterface 定义。
关于c# - 泛型和接口(interface)枚举,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/311274/