Forum Xamarin.Forms
We are excited to announce that the Xamarin Forums are moving to the new Microsoft Q&A experience. Q&A is the home for technical questions and answers at across all products at Microsoft now including Xamarin!

We encourage you to head over to Microsoft Q&A for .NET for posting new questions and get involved today.

Xamarin.Forms deep copy an object

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.

Best Answers

Answers

  • MaxenceSAUNIERMaxenceSAUNIER USMember ✭✭✭

    up please

  • MaxenceSAUNIERMaxenceSAUNIER USMember ✭✭✭

    up again

  • MaxenceSAUNIERMaxenceSAUNIER USMember ✭✭✭

    up

  • JohnHardmanJohnHardman GBUniversity admin
    edited November 2017

    @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.

  • MaxenceSAUNIERMaxenceSAUNIER USMember ✭✭✭

    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,

  • JohnHardmanJohnHardman GBUniversity admin

    @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.

  • MaxenceSAUNIERMaxenceSAUNIER USMember ✭✭✭

    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.

  • JohnHardmanJohnHardman GBUniversity admin

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

  • MaxenceSAUNIERMaxenceSAUNIER USMember ✭✭✭

    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.

  • JulienRosenJulienRosen CAMember ✭✭✭✭

    you can just serialize and unseralize like @nadjib post above

  • 1xo21xo2 NZMember ✭✭✭

    @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.

Sign In or Register to comment.