C# копирование списка объектов без ссылки
Привет,
Моя проблема заключается в копировании объекта без ссылки.
Что я уже пробовал:
Я пытаюсь скопировать список объектов, подобных этому :
List<MyObject> myNewList = new List<MyObject>(myOldList);
Проблема в том, что мой новый список ссылается на старый список.
Вот мой объект "MyObject" :
/// <summary> /// Model to represent a trip. /// </summary> [Serializable] public class TripModel : BaseModel, IEquatable<TripModel>, IComparableModel { /// <summary> /// Initialises a new instance of the <see cref="TripModel" /> class. /// </summary> public TripModel() { this.PropertyBags = new SimpleSerializableDictionary<string, string>(); this.TripPlans = new SimpleSerializableDictionary<TripEnums.TripPlanType, TripPlanModel>(); this.PreviewMode = GeneralEnums.PreviewMode.Unchanged; } /// <summary> /// Gets or sets the direction of the trip. /// </summary> public TripEnums.TripDirection Direction { get; set; } /// <summary> /// Gets or sets the instantiation mode of the trip. /// </summary> public TripEnums.TripInstantiationMode InstantiationMode { get; set; } /// <summary> /// Gets or sets the number of the next trip. /// </summary> public int NextTripNumber { get; set; } /// <summary> /// Gets or sets the number of the previous trip. /// </summary> public int PreviousTripNumber { get; set; } /// <summary> /// Gets or sets the property bags of the trip. /// </summary> public SimpleSerializableDictionary<string, string> PropertyBags { get; set; } /// <summary> /// Gets or sets the service ID of the trip. /// </summary> public string ServiceId { get; set; } /// <summary> /// Gets or sets the state of the trip. /// </summary> public TripEnums.TripState State { get; set; } /// <summary> /// Gets or sets the ID of the trip. /// </summary> public string TripId { get; set; } /// <summary> /// Gets or sets the number of the trip. /// </summary> public int TripNumber { get; set; } public GeneralEnums.PreviewMode PreviewMode { get; set; } /// <summary> /// Gets or sets the trip plans of the trip. /// </summary> public SimpleSerializableDictionary<TripEnums.TripPlanType, TripPlanModel> TripPlans { get; set; } /// <summary> /// Indicates whether the current object is equal to another object of the same type. /// </summary> /// <param name="other">An object to compare with this object.</param> /// <returns>Returns true if the two objects are equal false otherwise.</returns> public bool Equals(TripModel other) { if (other == null) { return false; } return this.TripNumber == other.TripNumber && this.ServiceId == other.ServiceId && this.TripId == other.TripId && this.Direction == other.Direction && this.State == other.State && this.InstantiationMode == other.InstantiationMode && this.PreviousTripNumber == other.PreviousTripNumber && this.NextTripNumber == other.NextTripNumber && object.Equals(this.PropertyBags, other.PropertyBags) && object.Equals(this.TripPlans, other.TripPlans); } /// <summary> /// Determines whether the specified System.Object is equal to the current System.Object. /// </summary> /// <param name="obj">The System.Object to compare with the current System.Object.</param> /// <returns>Returns true if the two objects are equal false otherwise.</returns> public override bool Equals(object obj) { return obj != null && this.Equals(obj as TripModel); } /// <summary> /// Serves as a hash function for <see cref="TripModel" />. /// </summary> /// <returns>A hash code for the current <see cref="TripModel" />.</returns> public override int GetHashCode() { return new { this.TripNumber, this.ServiceId, this.TripId, this.Direction, this.State, this.InstantiationMode, this.PreviousTripNumber, this.NextTripNumber, this.PropertyBags, this.TripPlans }.GetHashCode(); } /// <summary> /// Adds an information to the object indicating /// if the object has been updated, deleted, inserted /// or unchanged. /// </summary> /// <param name="previewMode"></param> public void AddModification(GeneralEnums.PreviewMode previewMode) { this.PreviewMode = previewMode; } }
Спасибо за вашу помощь...
BillWoodruff
Глядя на ваш код: почему вы используете PropertyBags, когда то, с чем вы работаете, действительно простые классы POCO ?