Solved

Serialization Events [Serializing], [Serialized], etc

Posted on 2013-06-08
1
309 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

Question has a verified solution.

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

This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
For most people, the WrapPanel seems like a magic when they switch from WinForms to WPF. Most of us will think that the code that is used to write a control like that would be difficult. However, most of the work is done by the WPF engine, and the W…
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…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …

624 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