我不明白为什么我不能将AImpl实例设置为下面的IA之类的代码。一般来说,由于IA或IB是接口,因此无法实例化IA。
public interface IA<in T> where T : IB
{
void Hold(T ib);
}
public interface IB
{
}
public class BImpl : IB
{
}
public class AImpl : IA<BImpl>
{
private IB b;
public void Hold(BImpl ib)
{
this.b = ib;
}
}
public class Programme
{
public static List<IA<IB>> List= new List<IA<IB>>();
public static void Main(string[] args)
{
var b = new BImpl();
var a = new AImpl();
a.Hold(b);
List.Add(a);
//This is not allowed due to AImpl is not assignable to IA<IB>
}
}