我有以下扩展方法可以将一个集合中的元素添加到另一个集合中:
public static void AddRange<T>(this ICollection<T> collection, IEnumerable<T> list)
{
foreach (var item in list)
{
collection.Add(item);
}
}
如果 IEnumerable 列表与我尝试将其添加到的 ICollection 的类型相同,则此方法工作正常。但是,如果我有这样的事情:
var animals = new List<Animal>();
var dogs = new List<Dog>(); // dog is a subclass of animal
animals.AddRange(dogs); // this line has a compiler error, it can't infer the type
如果 IEnumerable 的类型是 T 类型的子类(或实现接口(interface)),我该如何修改我的扩展方法才能执行类似的操作?
最佳答案
这个方法会给你你想要的:
public static void AddRange<A,B>(
this ICollection<A> collection,
IEnumerable<B> list)
where B: A
{
foreach (var item in list)
{
collection.Add(item);
}
}
一些注意事项:
- 唯一可行的方法是为 B 使用派生自 A 的类型。例如,Dog 是 Animal 的子类,因此 AddRange 将起作用。这是由“where B: A”子句强制执行的。否则,ICollection.Add() 调用将失败,因为 B 的不兼容类型被传递到期望 A 的 ICollection 中。
- 没有太多必要将 A 的类型限制为 Animal 类型层次结构中的任何类型;扩展方法可以用在任何你有一种类型派生自另一种类型的地方。
- 这并不是编译器无法推断类型的真正问题。即使您在任何地方都明确传递了 A 和 B 的类型,您仍然会遇到编译器错误。
关于c# - 如何编写一个将不同类型作为参数的泛型方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1753920/