How do I copy an instance of an object?

后端 未结 4 1828
天命终不由人
天命终不由人 2020-12-30 05:22

I\'m trying to write some code that populates a List (actually, it\'s a series of Lists, but we can pretend it\'s just one List). The

4条回答
  •  孤独总比滥情好
    2020-12-30 06:15

    As others have said, you would need to make that copy, in some PinnacleStock-specific way:

    foreach (PinnacleStock ps in p.StockList.Where(x => x.ColorCode == "10" && 
                                                        x.PackageLength == 30.64))
    {
      for (int i = 1; i <= ps.OnOrder; i++)
      {
        PinnacleStock clone = ps.CopySomehow();  // your problem
        r.TryAddPackage((IPackage)clone);
      }
    }
    

    However, you might want to question whether this is the right solution. Do you really need a separate instance of the PinnacleStock? Does adding a PinnacleStock to a Rack really create a new, independent instance? Are you planning to modify or track each of these individual copies separately? Is it correct that now you will have PinnacleStock instances that don't appear in your StockList? Not knowing your domain or the semantics of PinnacleStock, it's hard to be sure, but you might want to consider, say, creating a PinnacleStockRackEntry object to represent an instance of a PinnacleStock -- depends on the intended semantics of course!

提交回复
热议问题