Solved

Serialization Events [Serializing], [Serialized], etc

Posted on 2013-06-08
1
303 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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For a while now I'v been searching for a circular progress control, much like the one you get when first starting your Silverlight application. I found a couple that were written in WPF and there were a few written in Silverlight, but all appeared o…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
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…

770 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