如何在C#中克隆泛型列表?我在C#中有一個對象的通用列表,并希望克隆這個列表。列表中的項目是可圈可點的,但似乎沒有一個選項可供選擇list.Clone().有什么簡單的辦法嗎?
3 回答

ibeautiful
TA貢獻1993條經驗 獲得超6個贊
List<YourType> newList = new List<YourType>(oldList);
ICloneable
List<ICloneable> oldList = new List<ICloneable>();List<ICloneable> newList = new List<ICloneable>(oldList.Count);oldList.ForEach((item) => { newList.Add((ICloneable)item.Clone()); });
ICloneable
ICloneable
.
ICloneable
List<YourType> oldList = new List<YourType>();List<YourType> newList = new List<YourType>(oldList.Count);oldList.ForEach((item)=> { newList.Add(new YourType(item)); });
ICloneable
YourType.CopyFrom(YourType itemToCopy)
YourType
.

侃侃無極
TA貢獻2051條經驗 獲得超10個贊
public static object DeepClone(object obj) { object objResult = null; using (MemoryStream ms = new MemoryStream()) { BinaryFormatter bf = new BinaryFormatter(); bf.Serialize(ms, obj); ms.Position = 0; objResult = bf.Deserialize(ms); } return objResult;}
[Serializable()]
- 3 回答
- 0 關注
- 914 瀏覽
添加回答
舉報
0/150
提交
取消