Re: serialization without preregistering subclasses



Why not just use the BinaryFormatter class? Serialize it to a MemoryStream and then return the byte array which the object is serialized to. The serialization engine will handle all of these issues for you.


--
- Nicholas Paldino [.NET/C# MVP]
- mvp@xxxxxxxxxxxxxxxxxxxxxxxxxxx

"Me" <noone@xxxxxxx> wrote in message news:pan.2007.10.12.00.05.10@xxxxxxxxxx
Hi.

Object serialization is a complicated issue that has few (good)
solutions. In languages such as C++ and C# the programmer needs a way to
unserialize an object to the type it was originally created. This usually
involves some (often convoluted) lookup method that requires all the
subclasses to be pre-registered in a lookup table or a static switch/case
structure. In my native language (C++) there is no elegant way to do this.

In C# we have reflection, and my question is how I can use reflection
information to rebuild a subclass object that has been previously
serialized. I prefer to do binary serialization where the serialized
object's binary representation starts with a (class name) key to tell what
type of class it originally was.

Consider the following:


public class message {
virtual byte [] serialize() {}
static message unserialize(byte[] a) {}
}

public class datamessage: message {}

public class controlmessage: message {}

public class adminmessage: message {}


How can I use reflection information to create an object of the correct
subclass for binary serialization of a message that starts with the
correct class name as a key to indicate what class the serialized data
belongs to?

Please don't go off on tangents about problems with subclass attributes
that are references to other objects because I intend do deep copies of
all subclass attribute objects so that point is moot.

Any public how-tos or FAQ documents that I should view to find out more
about this?

Thanks

.



Relevant Pages

  • serialization without preregistering subclasses
    ... Object serialization is a complicated issue that has few ... In languages such as C++ and C# the programmer needs a way to ... public class datamessage: message ... subclass for binary serialization of a message that starts with the ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: A serialization problem
    ... The serialization seems to work OK (at least, ... > FileStream fileStream = new ... > BinaryFormatter formatter = new BinaryFormatter; ... > Gossip TeleCard ö<ÂÿÿÿÐÿÿÿ?GG05 - Oz Experience Phonecard ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Newbie Q: Serialization and Me
    ... Dim bf As BinaryFormatter = New BinaryFormatter ... I have 2 objects and I want to make a deep copy of one into the ... Target as T) as Integer 'use serialization to make a deep ...
    (microsoft.public.dotnet.languages.vb)
  • A serialization problem
    ... The serialization seems to work OK (at least, ... public class PersistantSettings ... BinaryFormatter formatter = new BinaryFormatter; ... Gossip TeleCard ö<ÂÿÿÿÐÿÿÿ?GG05 - Oz Experience Phonecard ...
    (microsoft.public.dotnet.languages.csharp)
  • RE: Challenge? [De-]Serialise a class derived from DataTable.
    ... Have you tried binary serialization? ... structures) when i deserialize them i get them back in the original ... Dim m As New MemoryStream ... Dim b As New BinaryFormatter() ...
    (microsoft.public.dotnet.languages.vb)