我試圖用克隆擴展克隆泛型列表。深層克隆如果列表項有另一個列表
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();
}
我有車對象實現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<Order>
所以克隆不爲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()'中寫更多的代碼來製作List的副本。這就是爲什麼你要首先實現這個功能,你知道;所以你可以照顧那樣的東西。 – Nyerguds