Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Serialization Events [Serializing], [Serialized], etc

Posted on 2013-06-08
1
Medium Priority
?
314 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 2000 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

Implementing Azure Infrastructure Exam 70-533

This course is designed to familiarize and instruct students in the content that is covered by Microsoft Exam 70-533, Implementing Microsoft Azure Solutions. It focuses on all the November 2016 objective domain topics.

Question has a verified solution.

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

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
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…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…

715 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