繁体   English   中英

如果列表项有另一个列表,则深度克隆

[英]Deep Clone If List Item Has Another List

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

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();
    }
}

扩展克隆我的列表,但车辆对象具有类型为List<Order> FilledOrders属性,因此克隆不会对 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();
    }
}

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

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项,也IClonableProduct类中实现IClonable并在那里做同样的事情。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM