0

I need to clone a list of object in Xamarin. Forms a project. The list is bounded to a View:

<StackLayout
   BindableLayout.ItemsSource="{Binding list1}" 
   BindableLayout.ItemTemplateSelector="{Binding MyTemplateSelector}" />

I already saw many ways but no one works in my environment.

I have used this function to clone a list:

public static List<T> Clone<T>(this List<T> oldList)
{
    var newList = new List<T>(oldList.Capacity);
    newList.AddRange(oldList);
    return newList;
}

Then I use it in this way

list2 = list1.Clone();

This works, if I add or remove some elements to list1; in list2 the elements remains the same.

But if I modify the value of a element's property, the edited value is copied to list2.

How can I solve this?

EDIT: This is a class that I need to copy

[VolosClass, Serializable, PropertyChanged.DoNotNotify]
public class WorkOrderChecklistDettaglio : BaseModel
{
    public double? Progressivo_Vis { get; set; } // progr_vis
    public double? Progressivo { get; set; } // progr
    public double? Progressivo_Unico_Checklist { get; set; } // progr_unica_checklist

    public string CodiceChecklist { get; set; } // codckl
    public string GuidDettaglio { get; set; } // guid_dett
    public string GuidDettaglioPadre { get; set; } // guid_dett_padre

    public string Descrizione { get; set; } // descr
    public string DescrizioneProdotto { get; set; } // descr_prodotto

    /// <summary>
    /// tabella standard CKT
    /// C=campo F=flag N=nessuno T=tabella
    /// codice
    /// </summary>
    public string TipoCampo { get; set; } // tipo_oper
    public string TipoCampo_2 { get; set; } // tipo_oper2

    /// <summary>
    /// tabella standard SCH
    /// tabella generica per checklist
    ///codice
    /// </summary>
    public string CodTab { get; set; } // cod_tab
    public string CodTab_2 { get; set; } // cod_tab2

    public string Flag { get; set; }
    public string Flag_2 { get; set; }

    public string Ris { get; set; }
    public string Ris_2 { get; set; }

    public string Valore { get; set; }
    public string Valore_2 { get; set; }

    public string Obbligatorio { get; set; } // ind_obb
    public string Obbligatorio_2 { get; set; } // ind_obb2

    public int? NumeroAllegati { get; set; } // contaAllegati
    public int? NumeroNote { get; set; } // contaNote

    #region Costruzione TreeView
    public int Livello { get; set; } = -1;
    public List<WorkOrderChecklistDettaglio> Children { get; set; }
    public bool IsRequired { get; set; } = false;
    #endregion
}
Hikari
  • 589
  • 7
  • 29
  • 4
    I think you are looking to make a "deep copy"? – UnholySheep Jul 29 '20 at 13:01
  • 2
    The problem you will find with deep-copy of an arbitrary object of type `T` is that it might not be easy to deep copy it. Imagine, for example, trying to deep-copy a `RichTextBox` that has a bunch of hooked-up event handlers. What are the types of objects in the array? Are they amenable to deep-copying? From the looks of it (a View?) they are not. – Matthew Watson Jul 29 '20 at 13:04
  • @MatthewWatson I have added the class that I need to copy. – Hikari Jul 30 '20 at 08:11
  • `List Children` is going to cause major issues. It could be arbitrarily deep. Also, we don't know what is in `BaseModel`. It would be hard to recursively copy all the members of that without using reflection. – Matthew Watson Jul 30 '20 at 14:13

0 Answers0