无法将类型为“object {System.Collections.Generic.List }”的对象强制转换为ICollection

为什么不能将运行时具有类型object {System.Collections.Generic.List}ICollectionICollection

我确定它显而易见但我无法弄明白。

它给出了例外 –

无法将类型为’System.Collections.Generic.List 1[System.String[]]' to type 'System.Collections.Generic.ICollection对象强制1[System.String[]]' to type 'System.Collections.Generic.ICollection 1 [System.Object]’。

在C#4.0之前,generics不是协变的。 如果你没有使用C#4.0,你可以像这样完成这个演员:

 List list = new List(); //...Fill the list... ICollection collection = list.ConvertAll(item => item as object); 

请参阅通用接口中的差异