好的,这有点难问,但我会尝试的。我有一个带有对象(Lots)的列表,其中再次包含一个带有对象(Wafer)的列表。当我更改晶圆中的值时,两个列表中的值都会更改!这就是我想要的。但是当我想从复制列表中删除一个晶圆时,它不应该从原始列表中删除。所以我想在每个批次中都有一个新的晶圆列表,但是对晶圆的引用应该与原始批次中的相同,因为我想更改晶圆的值,它应该更改原始晶圆和复制晶圆中的值. 没有深拷贝有可能吗?
我有以下代码,可以更好地解释它:
public class Lot
{
public string LotName { get; set; }
public List<Wafer> Wafers { get; set; }
}
public class Wafer
{
public string WaferName { get; set; }
}
[Test]
public void ListInListTest()
{
//Some Testdata
List<Lot> lotList = new List<Lot>();
Lot lot = new Lot();
lot.LotName = "Lot1";
lot.Wafers = new List<Wafer>();
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>();
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<Lot> 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<Lot> CopyList(List<Lot> lotList)
{
List<Lot> result = new List<Lot>(lotList);
foreach (Lot lot in result)
{
lot.Wafers = new List<Wafer>(lot.Wafers);
}
return result;
}
我希望它不会那么混乱?我希望我的问题得到足够的解释。