假设我有一些泛型驱动的基本接口(interface):
public interface Inteface<T> {
void Foo(T t);
}
现在我有了这个接口(interface)的一些具体实现,它也是通用的:
public class InterfaceImpl<T> {
public void Foo(T t) {
// Whatever
}
}
这看起来不错,但现在假设我有其他类(class):
public class Ololo {
public void BadFunction<TShouldModelInterface>(TShouldModelInterface shouldModelInterface) {
// Whatever
}
}
假设我想检查 TShouldModelInterface
实际上实现了任何可能的 Interface<T>
.
如果接口(interface)不是通用的,我会简单地写类似 where TShouldModelInterface : Interface
的东西.
但是如果接口(interface)是declared as Interface<T>
,有什么办法可以解决这个问题吗? ?
最佳答案
public class Ololo {
public void BadFunction<TShouldModelInterface, T>(TShouldModelInterface shouldModelInterface)
where TShouldModelInterface : Interface<T>
{
// Whatever
}
}
关于C#泛型问题——泛型接口(interface)约束,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4408208/