好的,这有点难以问,但我会尝试.我有一个带有对象的列表(Lots),它再次包含一个带有对象的列表(Wafers).当我更改晶圆内的值时,它将在两个列表中更改!这就是我想要的.但是当我想从复制的列表中删除晶圆时,不应该从原始列表中删除它.所以我想在每个批次中都有一个新的晶圆列表,但是对晶圆的参考应该与原始批次相同,因为我想要更改晶圆的值,它应该改变原始晶圆和复制晶圆的值.没有深层复制可能吗?
我有以下代码,以便更好地解释它:
public class Lot { public string LotName { get; set; } public ListWafers { get; set; } } public class Wafer { public string WaferName { get; set; } } [Test] public void ListInListTest() { //Some Testdata List lotList = new List (); Lot lot = new Lot(); lot.LotName = "Lot1"; lot.Wafers = new List (); Wafer wafer = new Wafer(); wafer.WaferName = "Wafer1"; lot.Wafers.Add(wafer); wafer = new Wafer(); wafer.WaferName = "Wafer2"; lot.Wafers.Add(wafer); wafer = new Wafer(); wafer.WaferName = "Wafer3"; lot.Wafers.Add(wafer); wafer = new Wafer(); wafer.WaferName = "Wafer4"; lot.Wafers.Add(wafer); lotList.Add(lot); lot = new Lot(); lot.LotName = "Lot1"; lot.Wafers = new List (); wafer = new Wafer(); wafer.WaferName = "Wafer1"; lot.Wafers.Add(wafer); wafer = new Wafer(); wafer.WaferName = "Wafer2"; lot.Wafers.Add(wafer); wafer = new Wafer(); wafer.WaferName = "Wafer3"; lot.Wafers.Add(wafer); wafer = new Wafer(); wafer.WaferName = "Wafer4"; lot.Wafers.Add(wafer); lotList.Add(lot); //Copy the List List copyList = CopyList(lotList); //That works. It removes the lot just in the copyList but not in //the original one copyList.RemoveAt(1); //This works not like i want. It removes the wafers from the copied list //and the original list. I just want, that the list will be changed //in the copied list copyList[0].Wafers.RemoveAt(0); } private List CopyList(List lotList) { List result = new List (lotList); foreach (Lot lot in result) { lot.Wafers = new List (lot.Wafers); } return result; }
我希望不是那么混乱?我希望我的问题解释得很好.
我想我可以看到你的问题在这里.在CopyList
你的有效克隆列表即
lot.Wafers = new List(lot.Wafers);
然而,关键的一点,这里要注意的是引用的Lot
对象仍然是相同的-所以有效地你是克隆和替换Wafers
原有的列表属性为好.
你打电话的时候
copyList.RemoveAt(1);
这很好,因为你正在操纵Lot
列表的副本.但是,当你打电话
copyList[0].Wafers.RemoveAt(0);
您正在修改两个列表仍在引用的Lot
实例.要解决这个问题,您需要克隆Lot
对象本身以有效地更改引用,即
Listresult = new List (lotList.Count); foreach (Lot item in lotList) { result.Add(new Lot() { LotName = item.LotName, Wafers = new List (item.Wafers) }); }
因此,您将丢失Lot
对象本身的两个列表中的自动更新,但您仍将保留它以修改单个Wafer
对象(因为它们的引用不会更改).
总而言之,您实际要问的是:
如何在具有不同属性引用的同时保留相同的对象引用?
这个问题的答案是 - 你做不到.