Joh*_*lan 1 .net c# generics ienumerable interface
如果有一组所有实现接口的类.
interface IMyinterface<T>
{
int foo(T Bar);
}
Run Code Online (Sandbox Code Playgroud)
我想把它们全部列入清单并通过它们进行枚举.
List<IMyinterface> list
foreach(IMyinterface in list)
// etc...
Run Code Online (Sandbox Code Playgroud)
但编译器想知道T是什么.我可以这样做吗?我怎样才能克服这个问题?
没有IMyinterface类型只有一个IMyinterface`1类型需要一个类型参数.你可以创建一个IMyinterface类型: -
interface IMyinterface { ... }
Run Code Online (Sandbox Code Playgroud)
然后继承它
interface IMyinterface<T> : IMyinterface { ... }
Run Code Online (Sandbox Code Playgroud)
您需要将要在foreach循环中使用的任何成员移动到IMyinterface定义.
| 归档时间: |
|
| 查看次数: |
198 次 |
| 最近记录: |