0
我收到以下錯誤如何管理C#泛型類,其中類型是基類的容器?
爲`System.Collections.Generic.List>。新增(myContainer中)」的最佳重載的方法匹配具有一些無效參數(CS1502)(GenericsTest)
對於下面的類:
A和B是MyBase的子類。
public class GenericConstraintsTest
{
private MyList<MyContainer<MyBase>> myList = new MyList<MyContainer<MyBase>>();
public GenericConstraintsTest()
{
MyContainer<A> ca = new MyContainer<A>(new A());
this.Add<A>(new A());
this.Add<B>(new B());
}
public void Add<S> (S value) where S : MyBase
{
MyContainer<S> cs = new MyContainer<S>(value);
myList.Add(cs);
}
public static void Main()
{
GenericConstraintsTest gct = new GenericConstraintsTest();
}
}
我在做什麼錯?
乾杯
+1:我的答案准確,一直到'IMyContainer'。 – 2011-06-11 04:31:42
+1您應該補充說明,這僅適用於.NET4(無論隨附的C#版本如何)。 – ChrisWue 2011-06-11 07:45:01