如果我有这样的方法:
public void Foo<T1, T2>(T1 list)
where T1 : IList<T2>
where T2 : class
{
// Do stuff
}
现在,如果我有:
IList<string> stringList = new List<string>();
List<object> objectList = new List<object>();
IList<IEnumerable> enumerableList = new List<IEnumerable>();
然后编译器无法解析要选择的泛型,这将失败:
Foo(stringList);
Foo(objectList);
Foo(enumerableList);
而且您必须明确指定要使用的泛型:
Foo<IList<string>, string>(stringList);
Foo<IList<object>, object>(objectList);
Foo<List<object>, object>(objectList);
Foo<IList<IEnumerable>, IEnumerable>(enumerableList);