比方說,我有List<Cookie>
,我想將它轉換爲CookieCollection
。最簡單的方法是什麼?將轉換列表<Cookie>轉換爲C#中的CookieCollection
我知道我可以使用一個foreach循環,但是沒有辦法用類似於這個的代碼來實例化它嗎?
List<Cookie> l = ...;
var c = new CookieCollection() { l };
當我嘗試編譯,雖然,我得到的錯誤:
The best overloaded Add method 'System.Net.CookieCollection.Add(System.Net.CookieCollection)' for the collection initializer has some invalid arguments
順便說一句,有CookieCollection
支持兩種Add
方法:
public void Add(Cookie cookie);
public void Add(CookieCollection cookies);
確實CookieCollection有一個構造函數一個IEnumerable或ICollection的? –
Sapph
2010-01-05 23:08:59
不,唯一的構造函數不帶參數。 – Senseful 2010-01-05 23:13:53
你可能會考慮看看這個(可能)[重複問題](http://stackoverflow.com/questions/524428/cannot-implicitly-convert-listt-to-collectiont)。 – 2010-01-05 23:10:42