如何在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());
});ICloneableICloneable.
ICloneable
List<YourType> oldList = new List<YourType>();List<YourType> newList = new List<YourType>(oldList.Count);oldList.ForEach((item)=>
{
newList.Add(new YourType(item));
});ICloneableYourType.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 关注
- 924 浏览
添加回答
举报
0/150
提交
取消
