【问题标题】:Deep Clone If List Item Has Another List如果列表项有另一个列表,则深度克隆
【发布时间】:2016-07-11 20:39:32
【问题描述】:

我正在尝试使用克隆扩展名克隆通用列表。

List<Vehicle> newList = currentVehicleList.Clone().ToList();

public static IList<T> Clone<T>(this IList<T> listToClone) where T : ICloneable
{
    return listToClone.Select(item => (T)item.Clone()).ToList();
}

我已经实现了 Vehicle 对象ICloneable

public class Vehicle : ICloneable
{
    [Key]
    public int VehicleId { get; set; }
    public string Plaka { get; set; }
    public double Volume { get; set; }
    public double FilledVolume { get; set; }
    public double DepartureTime { get; set; }
    public double IdleVolume
    {
        get { return this.Volume - this.FilledVolume; }
        set { }
    }
    public double FilledWeight { get; set; }
    public double IdleWeight {
        get { return this.WeightCapacity - this.FilledWeight; } 
        set{}
    }
    public decimal ConstantCost { get; set; }
    public string VehicleType { get; set; }
    public string Model { get; set; }
    public bool IsRent { get; set; }
    public double AvgVelocity { get; set; }
    public decimal CostPerKilometer { get; set; }
    public double WeightCapacity { get; set; }
    public bool InProgress { get; set; }
    public List<Order> FilledOrders { get; set; }

    public object Clone()
    {
        return this.MemberwiseClone();
    }
}

扩展程序克隆我的列表,但车辆对象的 FilledOrders 属性类型为 List&lt;Order&gt;,因此克隆不适用于 FilledOrders 列表。

public class Order : ICloneable
{
    [Key]
    public int OrderId { get; set; }
    public Customer Customer { get; set; }
    public bool IsTwoWayDirection { get; set; }
    public DateTime DeliveryDate { get; set; }
    public Dictionary<Product,int> OrderedProducts { get; set; }
    public Dictionary<Product, int> ReOrderedProducts { get; set; }
    public Double DemandTotalWeigth { get; set; }
    public Double DemandTotalVolume { get; set; }
    public Double PickupTotalWeigth { get; set; }
    public Double PickupTotalVolume { get; set; }
    public double EarliestArrivedTime { get; set; }
    public double ArrivedTime { get; set; }
    //TODO :Timespana cevrilecek
    public Double AcceptStartDate { get; set; }
    public Double AcceptEndDate { get; set; }
    public Double ServiceTime { get; set; }
    //

    public object Clone()
    {
        return this.MemberwiseClone();
    }
}

如果列表项有另一个列表,克隆的解决方案是什么?

【问题讨论】:

  • 只是...自己做。在Clone() 中编写更多代码以制作列表的副本。这就是为什么你首先要实现这个功能,你知道的;这样你就可以处理这样的事情了。

标签: c# asp.net deep-copy


【解决方案1】:

Vehicle 类中,只需扩展您的Clone() 函数以克隆列表:

public object Clone()
{
    Vehicle clone = this.MemberwiseClone();
    List<Order> clonedOrders = new List<Order>();
    foreach (Order order in this.FilledOrders)
        clonedOrders.Add((Order)order.Clone());
    clone.FilledOrders = clonedOrders;
    return clone;
}

毕竟,这就是实现 IClonable 的全部意义所在。否则你可以打电话给MemberwiseClone()

顺便说一下,Order 类中的 Dictionary 对象也不会被克隆,因为它们也是引用类型。要解决这个问题,原理是一样的:扩展OrderClone() 函数并在其中手动创建它们的副本。如果甚至需要克隆那个人的 Product 项目,请在 Product 类中实现 IClonable 并在那里执行相同操作。

只要不存在循环引用,您就可以继续实现IClonable 并在Clone() 函数中修复引用类型。

【讨论】:

  • 是的,它对我来说是一个解决方案。我试过了。 ;谢谢@Nyerguds。
  • @erkandemir 我编辑了答案,顺便说一句。不确定MemberwiseClone() 如何处理Order 类中的那些字典。但正如我所说,那里的原理是完全一样的。
猜你喜欢
  • 2019-03-25
  • 1970-01-01
  • 2013-01-06
  • 2013-09-06
  • 2017-11-03
  • 1970-01-01
  • 2013-10-11
  • 2023-04-03
  • 1970-01-01
相关资源
最近更新 更多