如何将List<object>
强制转换为List<SomethingElse>
?
(其中SomethingElse
已知是从object
继承而来)
额外的闲聊
转换列表:
List<Object> first = ...;
List<SomethingElse> second = (List<SomethingElse>)first;
无法转换类型'System.Collections.Generic.List'为'System.Collections.Generic.List'
尝试进行列表强制类型转换:
List<SomethingElse> second = first.Cast<SomethingElse>();
无法隐式转换类型'System.Collections.Generic.List'为'System.Collections.Generic.List'
不起作用:
我实际上并不需要完整的List<T>
对象,只需要一个ICollection<T>
即可:
ICollection<SomethingElse> second = first;
ICollection<SomethingElse> second = (ICollection<SomethingElse>)first;
ICollection<SomethingElse> second = first.Cast<SomethingElse>();
不起作用。