Cannot serialize fields - Portable Class Library / SharpSerializer for WinRT

Nov 8, 2013 at 7:21 AM
Edited Nov 8, 2013 at 12:09 PM
Hi! I'm trying to use sharpserializer to serialize my objects and store them as binary. First, thank you soooooo much for creating this extension of sharpserializer that also works for winrt. And second, well.. I'm just following the example on the sharpserializer tutorial trying to first of all save my object as xml (not binary) just to see it working. The lines from the tutorial that I'm using on my saveObject method is
void  saveObject (MyObject Obj){

      StorageFile file = null;
            string fileName = "my_file_name";

                StorageFolder folder = await GetFolder(storageType.Local, "subFolderName");
                file = await folder.CreateFileAsync(fileName, CreationCollisionOption.ReplaceExisting);
                IRandomAccessStream writeStream = await file.OpenAsync(FileAccessMode.ReadWrite);

  using (Stream outStream = Task.Run(() => writeStream.AsStreamForWrite()).Result)

                    // create instance of sharpSerializer
                    // with standard constructor it serializes to xml
                    var serializer__ = new Polenter.Serialization.SharpSerializer();

                    // serialize
                    serializer__.Serialize(Obj, outStream);
But the problem is after calling this method, I check the created file and I only have an empty object.

My file content is:

<Complex name="Root" type="Mynamespace.MyObject, ..." />

Any help? Thanks =)
Nov 8, 2013 at 12:13 PM
Updating.. I found the reason! The problem is that MyObject has only fields and fields are not serialized by sharpSerializer.
All fields that we want to be serialized must have a get accessor (a property that returns its value).

So now I'm gonna try to understand why sharpSerializer was created with this principle and I'm gonna check the possibility of adding some code to it that make the fields also get serialized (would be easier for me than having to update all my clases).
Mar 21, 2014 at 4:29 PM
Hi, SharpSerializer serializes only objects with default constructor and only its public properties. It cannot serialize private members and cannot serialize public fields.