我有以下类结构:
public class A : AInterface { }
public interface AInterface { }
public class B<T> : BInterface<T> where T : AInterface
{
public T Element { get; set; }
}
public interface BInterface<T> where T : AInterface
{
T Element { get; set; }
}
public class Y : B<A> { }
public class Z<T> where T : BInterface<AInterface> {}
public class Test
{
public Test()
{
Z<Y> z = new Z<Y>();
}
}
在C# 4.0中,我遇到了以下编译错误:
类型'Test.Y'不能用作泛型类型或方法'Test.Z'中的类型参数'T'。从'Test.Y'到'Test.BInterface'没有隐式引用转换。
我认为泛型协变应该可以解决这个问题?感谢您的帮助。
public class B : BInterface where T : AInterface
{
public T Element { get; set; }
}
public interface BInterface where T : AInterface
{
T Element { get; set; }
}
out将不起作用,因为T既在输入又在输出。有什么建议? - roger