鉴于:
public static void DoStuff<T>(ICollection<T> source)
{
Customer c = new Customer();
....
source.Add(c);
}
exceptc
不是类型<T>
。
那么如何将项目添加到通用集合中?
我尝试过:
public static void DoStuff(ICollection<Human> source)
{
Customer c = new Customer();
....
source.Add(c);
}
但我不使用它,因为没有人可以打电话DoStuff
:
ICollection<Customer> c;
DoStuff(c); <---error
因为一些关于协方差的东西,而.NET 没有意识到它Customer
来自Human
:
class Customer : Human {}