Solved

Serialization Events [Serializing], [Serialized], etc

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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Visual Studio editor? 1 38
C# Offline Apllication 5 56
VB.Net How to Exit Sub - Exit Form??? 5 51
SQL Login 17 40
For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
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…
Both in life and business – not all partnerships are created equal. As the demand for cloud services increases, so do the number of self-proclaimed cloud partners. Asking the right questions up front in the partnership, will enable both parties …

867 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