Serialization Question
-
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 aBinaryFormatter
) 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 (assumingName
is a member ofMyClass
that gets it's value from the constructor):// Will str be equal to "Hello"?
string str = MyClassCollection[0].Name;- 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 aBinaryFormatter
) 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 (assumingName
is a member ofMyClass
that gets it's value from the constructor):// Will str be equal to "Hello"?
string str = MyClassCollection[0].Name;- monrobot13
Serialization and deserialization of
MyClassCollection
(and the instances ofMyClass
that it holds) will work properly so long as you applySerializableAttribute
to bothMyClassCollection
andMyClass
. If either class is not marked withSerializableAttribute
you will receive aSerializationException
when you try to callBinaryFormatter.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.
-
Serialization and deserialization of
MyClassCollection
(and the instances ofMyClass
that it holds) will work properly so long as you applySerializableAttribute
to bothMyClassCollection
andMyClass
. If either class is not marked withSerializableAttribute
you will receive aSerializationException
when you try to callBinaryFormatter.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.
Kastro wrote: Hope this helps That helps a lot. Thanks for the help. - monrobot13