私は次のクラス構造を持っています:
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'への暗黙の参照変換はありません。
ジェネリックスの共分散はこれを機能させるはずですが?どんな助けでもありがたいです。