Serialization

I'm pretty confused as to how it all works.  I have a class which inherits TabControl and I want to be able to serialize it's data so I can deserealize it at a later date.

So I started off with:
namespace MyNameSpace
{
    [Serializable]
    public class MyClass: TabControl
    {
...

The problem with that was it didn't like the fact TabControl is not Serializable.  So looking on google I found you can implement
ISerializable.  This is fine and writing a GetObject method certainly serialized something but I'm pretty certain it didn't searliaze the whole object.  I now have:

namespace UserFriendlyLaTeX
{
    [Serializable]
    public class Project : TabControl, ISerializable
    {
       public void GetObjectData(SerializationInfo info, StreamingContext context)
        {
            this.info = info;
            this.context = context;
            // implementation code goes here
        }
...

What should I be putting in the "implementation code goer here" part   I saw something about Binary Streams etc but I am lost as to what it all means.  A simple explination as to how I can go about serializing object which inherit or use other objects would be much appreciated.


Answer this question

Serialization

  • Kyra

    When you serialize an object, every field in that object must be able to be serialized, that is, either a primitive type, or reference another type that is also serializable.

    The implementation is filled out with something like the below example (from one of my older classes using a binary formatter...):

    Where say, "FileFormat" is the serialzation name you would use with SerialzationInfo.GetString (because it happens to e a string here).

    I don't know if I would do it this way now, I think I'd use an XmlSerializer, which is human readable, and a bit cleaner (I think) to code, though somewhat slower.

    public virtual void GetObjectData(SerializationInfo info, StreamingContext context)
    {
        //Version Settings
       
    info.AddValue("PreferencesVersion", (int)1);

        //Version { 1 } Settings
       
    info.AddValue("FileFormat", fileFormat);
        info.AddValue("DefaultSavePath", defaultSavePath);
        info.AddValue("DefaultUri", defaultUri);
    }



  • Serialization