Solved

Serialization Events [Serializing], [Serialized], etc

Posted on 2013-06-08
1
298 Views
Last Modified: 2013-06-09
It looks like Serialization Events [Serializing], [Serialized], [Deserializing], and [Deserialized]
were not applicable in .NET Framework 2.0 for a SoapFormatter serialization according to my MCTS 70-536 App Development  .NET Framework Foundation book. It says only BinaryFormatter Serialization can use Serialization attributes [Serializing], [Serialized], etc.

The question is what version of .NET Framework was SoapFormatter Serialization able to use Serialization Attributes [Serializing], [Serialized], [Deserializing], and [Deserialized]?

0
Comment
Question by:metro156
1 Comment
 
LVL 42

Accepted Solution

by:
sedgwick earned 500 total points
ID: 39232639
the following classes were introduced in .net 2.0 and supported by SoapFormatter:
OnSerializedAttribute
OnDeserializedAttribute
OnDeserializingAttribute
OnSerializingAttribute

if you run the code below, u see that all serialization attributes are being supported and added to the static events list.

check this code:
[Serializable]
    public class SoapFormatterTest
    {
        //store serialization events
        public static List<string> events = new List<string>();

        [OnSerialized()]
        internal void OnSerialized(StreamingContext context)
        {
            events.Add("OnSerialized");
        }
        [OnDeserialized()]
        internal void OnDeserialized(StreamingContext context)
        {
            events.Add("OnDeserialized");
        }
        [OnSerializing()]
        internal void OnSerializing(StreamingContext context)
        {
            events.Add("OnSerializing");
        }
        [OnDeserializing()]
        internal void OnDeserializing(StreamingContext context)
        {
            events.Add("OnDeserializing");
        }

        public string Name = "SF";
        private int ID = 1234;
        public Hashtable addresses = new Hashtable();

        public SoapFormatterTest()
        {
            addresses.Add("Jeff", "123 Main Street, Redmond, WA 98052");
            addresses.Add("Fred", "987 Pine Road, Phila., PA 19116");
            addresses.Add("Mary", "PO Box 112233, Palo Alto, CA 94301");
        }

        public void Deserialize()
        {
            FileStream fs = new FileStream(@"c:\temp\DataFile.soap", FileMode.Open);

            SoapFormatter formatter = new SoapFormatter();
            try
            {
                SoapFormatterTest instance = (SoapFormatterTest)formatter.Deserialize(fs);
            }
            catch (SerializationException e)
            {
                Console.WriteLine("Failed to serialize. Reason: " + e.Message);
                throw;
            }
            finally
            {
                fs.Close();
            }
        }

        public void Serialize()
        {
            FileStream fs = new FileStream(@"c:\temp\DataFile.soap", FileMode.Create);

            SoapFormatter formatter = new SoapFormatter();
            try
            {
                formatter.Serialize(fs, this);
            }
            catch (SerializationException e)
            {
                Console.WriteLine("Failed to serialize. Reason: " + e.Message);
                throw;
            }
            finally
            {
                fs.Close();
            }
        }

    }

Open in new window


and usage:
        static void Main(string[] args)
        {
            SoapFormatterTest sf = new SoapFormatterTest();
            sf.Serialize();
            sf.Deserialize();
            foreach (var ev in SoapFormatterTest.events)
            {
                Console.WriteLine(ev);
            }
        }

Open in new window

0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
After several hours of googling I could not gather any information on this topic. There are several ways of controlling the USB port connected to any storage device. The best example of that is by changing the registry value of "HKEY_LOCAL_MACHINE\S…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now