Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / October 2007

Tip: Looking for answers? Try searching our database.

serialization without preregistering subclasses

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Me - 12 Oct 2007 01:05 GMT
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
Nicholas Paldino [.NET/C# MVP] - 12 Oct 2007 02:27 GMT
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.

Signature

         - Nicholas Paldino [.NET/C# MVP]
         - mvp@spam.guard.caspershouse.com

> Hi.
>
[quoted text clipped - 38 lines]
>
> Thanks

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.