C# Serialization Guide Part 3 – ISerializable Interface

14 בApril 2014

אין תגובות

Another popular way to use serialization is by customize the serialization environment

.But than the serialization results depends on your implementation

.This is often more complicated but indeed more flexible

:again lets used the base serialization Contract as we saw at former serialization posts

public interface ISerializeService

   {

       bool write(object data, Type type);

       object read(string path,Type t);

   }

:Now lets define the object we like to serialize

[Serializable]

    public class SerializeTest : ISerializable

    {

        public string Value;


        public SerializeTest()

        {

            Value = "i am serializble object";

        }


        public void GetObjectData(SerializationInfo info, StreamingContext context)

        {

            info.AddValue("ValueProperty", Value);

        }


        public SerializeTest(SerializationInfo info, StreamingContext context)

        {

            Value = (string)info.GetValue("ValueProperty", typeof(string));

        }

    }

:And for the details

First we can see that this object must implement the
GetObjectData(info, context) functions, meaning that every field we like to be including in the serialization pocket we are responsible to add it to SerializationInfo data, and doing that per

,current StreamContext instance

The second thing is the nonDefault constructor bur, this is nor must this is for deserialization

.case when we try to rebuild an object from the stream

.Third  after all of it yet we need to remark our object as [Serializable] attribute

.Now lets try to implement our base SerializationService Contract for Iserializable object

public class CustomSerializeService : ISerializeService

    {

        public bool write(object data, Type type,string path)

        {

            bool res = false;

            try

            {

                IFormatter formatter = new SoapFormatter();

                FileStream stream = new FileStream(path, FileMode.Create);

                formatter.Serialize(stream, data);

                stream.Close();

            }

            catch (Exception ex)

            {

                //deal with exception

            }

                 return res;

        }


        public object read(string path, Type t)

        {

            IFormatter formatter = new SoapFormatter();

            FileStream s = new FileStream(path, FileMode.Open);

            return formatter.Deserialize(s);

        }

    }

So, what we have here ? the first is define of Fomatter for serialize platform (aka binary or soap)

You need reference to System.Runtime.Serialization.Formatters.Soap

soap is an transformation Protocol which is non relevant here accept the fact that is build kind

.of xml doc. For debugging. Than we have fileStream to run the serialization on it

The reader part is quite simple s we used to serialize in former ways, the only different between

.Iserializable member and DataContractSerializer or XMLSerializer is the Formatter

:Well what we have left is to run the methods

serializer = new CustomSerializeService();


          serializer.write(new SerializeTest(), typeof(SerializeTest), "c://data3.txt");


          SerializeTest  fromXML =(SerializeTest)serializer.read("c://data3.txt", typeof(SerializeTest));

הוסף תגובה
facebook linkedin twitter email

Leave a Reply

Your email address will not be published. Required fields are marked *