哪有我投了List<object>
到List<SomethingElse>
?如何投放列表<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>();
不起作用。
相关? http://stackoverflow.com/questions/1266014/c-sharp-casting-a-listobjbase-as-listobj – eldarerathis
可能的重复http://stackoverflow.com/questions/7955890/how-to-cast-listclassb-to -listclassa-when-classb-inherits-from-classa – dash
这个工作吗? List second = first.Select(o =>(SomethingElse)o).ToList(); –
asmo