哪有我投了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