Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ComponentSerializationService not deserializing all properties

Tags:

c#

I'm using the ComponentSerializationService as found here:

When you cut a control from the design service the serialize method gets called, and when you paste deserialize.

In my example - I have a simple button and I've changed the background color.

Serialize looks like this :

public object Serialize(ICollection objects)
      {
         var serializationService = _serviceProvider.GetService(typeof(ComponentSerializationService)) as ComponentSerializationService;

         if (serializationService == null)
         {
            throw new Exception("ComponentSerializationService not found");
         }

         SerializationStore returnObject;

         using (var serializationStore = serializationService.CreateStore())
         {
            foreach (object obj in objects)
            {
               if (obj is Control)
               {
                  serializationService.Serialize(serializationStore, obj);
               }                  
            }

            returnObject = serializationStore;
         }

         return returnObject;
      }

And Deserialize looks like this :

public ICollection Deserialize(object serializationData)
      {
         var serializationStore = serializationData as SerializationStore;

         if (serializationStore == null)
         {
            return new object[] {};
         }

         var componentSerializationService = _serviceProvider.GetService(typeof(ComponentSerializationService)) as ComponentSerializationService;

         if (componentSerializationService == null)
         {
            throw new Exception("ComponentSerializationService not found");
         }

         var collection = componentSerializationService.Deserialize(serializationStore);

         return collection;
      }

I've set breakpoints in both methods and the incoming object contains the correct background property, but once deserialized the property is not persisted.

Any ideas welcome. This is a tricky class with very little in the line of code samples or documentation.

like image 952
JL. Avatar asked Mar 01 '16 11:03

JL.


1 Answers

You should take a look at this, and see if the SerializeAbsolute works for you :)

https://msdn.microsoft.com/en-us/library/system.componentmodel.design.serialization.componentserializationservice.serializeabsolute(v=vs.110).aspx

More specifically the Remark and I quote

Standard serialization, as implemented through the Serialize method, only serializes values that differ from the component's default state. This provides the most compact serialization mechanism but assumes that a newly created object will be used during deserialization. If an existing object is used, the resulting deserialized object is not guaranteed to duplicate the original state of the serialized object; the properties that contained default values during serialization will not be reset back to their defaults during deserialization. The SerializeAbsolute method does not use this shortcut. It serializes all properties of the source object so that deserialization can restore all the object's properties, regardless of default state.

like image 152
Blaatz0r Avatar answered Nov 14 '22 03:11

Blaatz0r