locked
Xamarin.Forms deep copy an object RRS feed

  • Question

  • User272646 posted

    Hello,

    I haved search and I do not have find a good response. So, my question is : How deep copy an object in Xamarin.Forms ?

    • BinaryFormatter do not exist in Xamarin.Forms
    • Can't install FastDeepCloner with 4.5 .NET
    • ICloneable do not exist in Xamarin.Forms

    Thanks for your answers.

    Tuesday, November 14, 2017 10:25 AM

Answers

  • User76049 posted

    @MaxenceSAUNIER

    It exists in the mono API.

    https://developer.xamarin.com/api/member/System.ICloneable.Clone/

    You need to write some dependency services to access this from Xamarin Forms, like cryptography you need to leverage dependency services to write some platform specific code to achieve this.

    it's also discussed in a forum thread here.

    https://forums.xamarin.com/discussion/55022/how-to-clone-a-list

    Xamarin Forms is a cross platform UI abstration, to use native features you need dependancy services but generally it's a small price to pay for writing your app once.

    As @JohnHardman says, you don't really explain why you need this.

    • Marked as answer by Anonymous Thursday, June 3, 2021 12:00 AM
    Thursday, November 16, 2017 9:57 AM
  • User74518 posted

    var clone = JsonConvert.DeserializeObject< YourObjectClass>(JsonConvert.SerializeObject(objectToBeCloned));

    You can create an extension method Clone< T> with that if you want.

    • Marked as answer by Anonymous Thursday, June 3, 2021 12:00 AM
    Monday, November 20, 2017 7:49 PM

All replies

  • User272646 posted

    up please

    Wednesday, November 15, 2017 8:50 AM
  • User272646 posted

    up again

    Wednesday, November 15, 2017 5:10 PM
  • User272646 posted

    up

    Thursday, November 16, 2017 9:15 AM
  • User89714 posted

    @MaxenceSAUNIER - Assuming that no library already exists to do this, how could you implement it yourself? It's a C# exercise, rather than being anything to do with Xamarin.Forms . Actually, it's a reasonable interview question for a C# role.

    I'm curious - do you need this for an app you are building? If so, why? That information may suggest an alternative way to proceed.

    Thursday, November 16, 2017 9:21 AM
  • User76049 posted

    @MaxenceSAUNIER

    It exists in the mono API.

    https://developer.xamarin.com/api/member/System.ICloneable.Clone/

    You need to write some dependency services to access this from Xamarin Forms, like cryptography you need to leverage dependency services to write some platform specific code to achieve this.

    it's also discussed in a forum thread here.

    https://forums.xamarin.com/discussion/55022/how-to-clone-a-list

    Xamarin Forms is a cross platform UI abstration, to use native features you need dependancy services but generally it's a small price to pay for writing your app once.

    As @JohnHardman says, you don't really explain why you need this.

    • Marked as answer by Anonymous Thursday, June 3, 2021 12:00 AM
    Thursday, November 16, 2017 9:57 AM
  • User272646 posted

    It's difficult to explain the reason but I can test. So,

    App process:

    1. When the manager makes a visit, the application repatriates the visits to be made.
    2. Visit object contain many parameters and a 'pictures' list.
    3. The server sends to the application the photos of the previous visit. So, visits already contain photos.
    4. The manager then takes on the spot new photos and signs the visit to synchronize with the server.

    Synchronisation:

    1. I need to generate a temporary variable during synchronization that is equal to the Visit object but without any old photos. 1.1. Send visit to the server to verification and id creation. Object sended = Visit without old pictures and without base64 data 1.2. Loop on they photos, open and generate base64 data and send it to server. 1.2. Valid and delete Visit.

    So, the problem is: If the Visit -- for any reason (internet connection or other) -- is not send to the server, the manager can resend the visit. But it must also be able to look at the pictures taken and the old photos during all the time or the visit is not yet fully synchronized. This is why I must be able to * temporarily * delete the old photos but just for sending so not lost locally. Hence my problem of being able to create a copy of the temporary object when sending the Visit.

    Thanks,

    Friday, November 17, 2017 9:00 AM
  • User89714 posted

    @MaxenceSAUNIER - Couldn't you just add one or more status flags for each photo, to indicate whether the photo has been successfully sent to the server, and (if necessary) whether it is an old photo?

    Alternatively, if it's just for this object, add a clone method that operates just on this object, rather than attempting to be completely generic. Whilst it's nice to have completely generic, reusable code, sometimes it's pragmatic to take an easier route.

    Friday, November 17, 2017 9:09 AM
  • User272646 posted

    I already have an indicator to not send it if they are already sent or if it contain a server ID. The problem is the first sending without data base64 which allows the assignment of ID to all new photographs. Do not send information from old photos. It is a service provider that sets up the API. So it would cost money.

    Friday, November 17, 2017 2:52 PM
  • User89714 posted

    @MaxenceSAUNIER - Can you not simply check whether a photo has an ID assigned?

    Friday, November 17, 2017 3:58 PM
  • User272646 posted

    Exactly, that's what I do. But the webservice asks that they are not present. If I remove them for sending on the API, they can not then view them.

    Friday, November 17, 2017 4:09 PM
  • User74518 posted

    var clone = JsonConvert.DeserializeObject< YourObjectClass>(JsonConvert.SerializeObject(objectToBeCloned));

    You can create an extension method Clone< T> with that if you want.

    • Marked as answer by Anonymous Thursday, June 3, 2021 12:00 AM
    Monday, November 20, 2017 7:49 PM
  • User169828 posted

    you can just serialize and unseralize like @nadjib post above

    Monday, November 20, 2017 8:37 PM
  • User343682 posted

    @Amar_Bait said: var clone = JsonConvert.DeserializeObject< YourObjectClass>(JsonConvert.SerializeObject(objectToBeCloned));

    You can create an extension method Clone< T> with that if you want.

    thnks, I love it.

    Thursday, September 26, 2019 7:54 AM