Serving the Quantitative Finance Community

 
User avatar
TheTiger
Topic Author
Posts: 0
Joined: September 17th, 2002, 8:25 am

Serialization in C#

December 20th, 2005, 5:31 pm

Guys, can you please help me with the following issue.I want to serialize objects in C# with minimum effort. Let's say I have the following classes:class ABase // this is a base clase{ public int x_; public ABase() { x_ = 0; }}class AChild : ABase // this one is derived from ABase{ public int y_; public AChild() { y_ = 0; }}class BBase // another base class{ public int z_; public BBase() { z_ = 0; }}class BChild : BBase // derived from BBase{ public ABase obj_; public BChild(ABase obj) { obj_ = obj; }}Now let's say that I have the objectBBase t = new BChild(new AChild());I want to use BinaryFormatter.Serialize() and BinaryFormatter.Deserialize() methods to serialize object t into a stream and deserialize it afterwards.My question is whether serialization is deep or shallow, whether virtualism is preserved after serialization/deserialization of the object, and what I should do to make serialization working properly. More specifically, I am concerned if my application will understand that the deserialized object is actually of type BChild and not BBase and if it understands that, will it understand that obj_ variable of the deserialized object is of type AChild and not of ABase?Please help ASAP! Thanks a lot in advance!P.S. The variables x_, y_, z_ are just for classes to have something inside.
Last edited by TheTiger on December 19th, 2005, 11:00 pm, edited 1 time in total.
 
User avatar
TheTiger
Topic Author
Posts: 0
Joined: September 17th, 2002, 8:25 am

Serialization in C#

December 20th, 2005, 8:10 pm

using System; using System.IO; using System.Runtime.Serialization; using System.Runtime.Serialization.Formatters.Binary; [Serializable] class ABase // this is a base clase { public string x_; public ABase() { x_ = "I am class ABase"; } public virtual void print() { Console.WriteLine(x_); } } [Serializable] class AChild : ABase // this one is derived from ABase { public string y_; public AChild() { y_ = "I am class AChild"; } public override void print() { Console.WriteLine(y_); } } [Serializable] class BBase // another base class { public string z_; public BBase() { z_ = "I am class BBase"; } public virtual void print() { Console.WriteLine(z_); } } [Serializable] class BChild : BBase // derived from BBase { public ABase obj_; public string w_; public BChild(ABase obj) { w_ = "I am class BChild"; obj_ = obj; } public override void print() { Console.WriteLine(w_); obj_.print(); } } static void testSerialization() { BBase t = new BChild(new AChild()); BinaryFormatter x = new BinaryFormatter(); Stream stream = File.Open("c:\\serialize.txt", FileMode.Create); x.Serialize(stream,t); stream.Close(); stream = File.Open("c:\\serialize.txt", FileMode.Open); BBase u = x.Deserialize(stream) as BBase; u.print(); } static void Main(string[] args) { System.Console.WriteLine("Starting testing..."); // put test code here testSerialization(); // test code ends before this comment System.Console.WriteLine("Testing has been finished...Press any key to continue."); System.Console.ReadLine(); }Actually it works (at least to some degree)! The output is below:Starting testing...I am class BChildI am class AChildTesting has been finished...Press any key to continue.
Last edited by TheTiger on December 19th, 2005, 11:00 pm, edited 1 time in total.
 
User avatar
Cuchulainn
Posts: 22926
Joined: July 16th, 2004, 7:38 am

Serialization in C#

December 21st, 2005, 9:41 am

/*My question is whether serialization is deep or shallow, whether virtualism is preserved after serialization/deserialization of the object, and what I should do to make serialization working properly. More specifically, I am concerned if my application will understand that the deserialized object is actually of type BChild and not BBase and if it understands that, will it understand that obj_ variable of the deserialized object is of type AChild and not of ABase? */THave you tried implementing the IDeserialisationCallback interface?class Person: IDeserialisationCallback {public OnDeserialization(object o){ // Your code}}I have the feeling that this might do the job.DanielEdit: this interface is standard so might be the best approach?
Last edited by Cuchulainn on December 20th, 2005, 11:00 pm, edited 1 time in total.
 
User avatar
Russell
Posts: 1
Joined: October 16th, 2001, 5:18 pm

Serialization in C#

December 22nd, 2005, 9:43 am

Hi Tiger,Serilaization is deep (each field in your class must also be serializable or you will get errors when you try and serialise).In you example above if you serialise a AChild even though it was contained in a variable defined as type ABase you will create a serialised object that *is* of type AChild.All you need to do is add the Serializable attribute to your classes i.e.[Serializable()]public class Test1{ ......}to load and save look at the System.Runtime.Serialization.Formatters.Binary namespace.rgds,Russ