Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calling GetObjectData on serialization

I am confused about the serialization sample from MSDN.

My confusion is in method GetObjectData (which is called during serialization), will the method,

  1. serialize both the additional data (in method GetObjectData from AddValue) and the fields/properties of the class;
  2. or just write the data in method GetObjectData without writing fields/properties of the class?

I have debugged seems (2) is correct -- no fields/properties data are serialized if GetObjectData method is used? Is that correct? (I am not an expert and just want to confirm here, but 100% confident about myself.)

like image 864
George2 Avatar asked Jan 24 '23 19:01

George2


1 Answers

Im not sure what you want to achieve but isn't easier to let C# do the work for you:

using System;
using System.Collections.Generic;
using System.Xml.Serialization;

namespace Test
{
    [Serializable]
    public class TestObject
    {
        private String name;
        private String note;
        #region Getters/setters

        public String Name
        {
            get { return name; }
            set { name = value; }
        }

        public String Note
        {
            get { return note; }
            set { note = value; }
        }
        #endregion
    }
}

Now you can use the XmlSerializer or BinaryFormatter to (de)serialize the object

like image 73
RvdK Avatar answered Jan 31 '23 03:01

RvdK