5
是可以进行以下不编译:投泛型类型的接口类型约束
- 制作
IFooCollection
通用 - 上
FooCollection
明确实施IFooCollection.Items
并进行显式类型转换。
public interface IFoo
{
}
public interface IFooCollection
{
IEnumerable<IFoo> Items { get; }
}
public class FooCollection<T> : IFooCollection where T : IFoo
{
public IEnumerable<T> Items { get; set; }
}
我与第二个解决方案开心就好(显式地实现接口),但想明白,为什么我需要投T
作为IFoo
当我们有一个通用的限制指定T
绝实施IFoo
。