Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Serialization Question

Serialization Question

Scheduled Pinned Locked Moved C#
questionjsontutorial
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    monrobot13
    wrote on last edited by
    #1

    Hi Everyone, Lets say I have a collection class (MyClassCollection) that holds instances of another class I've defined (MyClass). If I serialize the collection class (using a BinaryFormatter) and then deserialize it later will I get back the exact same information? For example if I had the following line:

    MyClassCollection[0] = new MyClass ("Hello");

    If I then serialize and deserialize the MyClassCollection will this work as expected (assuming Name is a member of MyClass that gets it's value from the constructor):

    // Will str be equal to "Hello"?
    string str = MyClassCollection[0].Name;

    - monrobot13

    K 1 Reply Last reply
    0
    • M monrobot13

      Hi Everyone, Lets say I have a collection class (MyClassCollection) that holds instances of another class I've defined (MyClass). If I serialize the collection class (using a BinaryFormatter) and then deserialize it later will I get back the exact same information? For example if I had the following line:

      MyClassCollection[0] = new MyClass ("Hello");

      If I then serialize and deserialize the MyClassCollection will this work as expected (assuming Name is a member of MyClass that gets it's value from the constructor):

      // Will str be equal to "Hello"?
      string str = MyClassCollection[0].Name;

      - monrobot13

      K Offline
      K Offline
      Kastro
      wrote on last edited by
      #2

      Serialization and deserialization of MyClassCollection (and the instances of MyClass that it holds) will work properly so long as you apply SerializableAttribute to both MyClassCollection and MyClass. If either class is not marked with SerializableAttribute you will receive a SerializationException when you try to call BinaryFormatter.Serialize. The following demonstrates the serialization of your classes:

      using System;
      using System.IO;
      using System.Runtime.Serialization;
      using System.Runtime.Serialization.Formatters.Binary;

      [Serializable()]
      public class MyClass {

      public MyClass(string name) {
      	\_Name = name;
      }
      
      public string Name {
      	get { return \_Name; }
      }
      
      private string \_Name;
      

      }

      [Serializable()]
      class MyClassCollection {

      public MyClassCollection() {
      	\_Items = new MyClass\[4\];
      }
      
      public MyClass this\[int index\] {
      	get { return \_Items\[index\]; }
      	set { \_Items\[index\] = value; }
      }
      
      public override string ToString() {
      	string result = "MyClassCollection {";
      	for (int i = 0; i < \_Items.Length; i++)
      		result += (i != 0 ? ", " : "") + (\_Items\[i\] != null ? \_Items\[i\].Name : "\[null\]");
      	result += "}";
      	return result;
      }
      
      private MyClass\[\] \_Items;
      
      \[STAThread\]
      static void Main(string\[\] args) {
      	BinaryFormatter formatter = new BinaryFormatter();
      	MemoryStream stream = new MemoryStream();
      	// Populate collection
      	MyClassCollection col = new MyClassCollection();
      	col\[0\] = new MyClass("Item #1");
      	col\[2\] = new MyClass("Item #2");
      	Console.WriteLine(col);
      	// Serialize collection and reset stream to beginning
      	formatter.Serialize(stream, col);
      	col = null;
      	stream.Seek(0, SeekOrigin.Begin);
      	// Deserialize collection
      	col = (MyClassCollection)formatter.Deserialize(stream);
      	stream.Close();
      	Console.WriteLine(col);
      	Console.Read();
      }
      

      }

      Output:

      MyClassCollection {Item #1, [null], Item #2, [null]}
      MyClassCollection {Item #1, [null], Item #2, [null]}

      Hope this helps.

      M 1 Reply Last reply
      0
      • K Kastro

        Serialization and deserialization of MyClassCollection (and the instances of MyClass that it holds) will work properly so long as you apply SerializableAttribute to both MyClassCollection and MyClass. If either class is not marked with SerializableAttribute you will receive a SerializationException when you try to call BinaryFormatter.Serialize. The following demonstrates the serialization of your classes:

        using System;
        using System.IO;
        using System.Runtime.Serialization;
        using System.Runtime.Serialization.Formatters.Binary;

        [Serializable()]
        public class MyClass {

        public MyClass(string name) {
        	\_Name = name;
        }
        
        public string Name {
        	get { return \_Name; }
        }
        
        private string \_Name;
        

        }

        [Serializable()]
        class MyClassCollection {

        public MyClassCollection() {
        	\_Items = new MyClass\[4\];
        }
        
        public MyClass this\[int index\] {
        	get { return \_Items\[index\]; }
        	set { \_Items\[index\] = value; }
        }
        
        public override string ToString() {
        	string result = "MyClassCollection {";
        	for (int i = 0; i < \_Items.Length; i++)
        		result += (i != 0 ? ", " : "") + (\_Items\[i\] != null ? \_Items\[i\].Name : "\[null\]");
        	result += "}";
        	return result;
        }
        
        private MyClass\[\] \_Items;
        
        \[STAThread\]
        static void Main(string\[\] args) {
        	BinaryFormatter formatter = new BinaryFormatter();
        	MemoryStream stream = new MemoryStream();
        	// Populate collection
        	MyClassCollection col = new MyClassCollection();
        	col\[0\] = new MyClass("Item #1");
        	col\[2\] = new MyClass("Item #2");
        	Console.WriteLine(col);
        	// Serialize collection and reset stream to beginning
        	formatter.Serialize(stream, col);
        	col = null;
        	stream.Seek(0, SeekOrigin.Begin);
        	// Deserialize collection
        	col = (MyClassCollection)formatter.Deserialize(stream);
        	stream.Close();
        	Console.WriteLine(col);
        	Console.Read();
        }
        

        }

        Output:

        MyClassCollection {Item #1, [null], Item #2, [null]}
        MyClassCollection {Item #1, [null], Item #2, [null]}

        Hope this helps.

        M Offline
        M Offline
        monrobot13
        wrote on last edited by
        #3

        Kastro wrote: Hope this helps That helps a lot. Thanks for the help. - monrobot13

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups