C# Object serialization

  • Thread starter Thread starter linuxfedora
  • Start date Start date
L

linuxfedora

I know how to serialize a object into a file. But if i want to
serialize several different objects into the same file and not at the
same time, Then how can i do it?Thanks
For example:

i need to serialize ClassA object first, then after 1 minute, i need
to serialize ClassB objec to the same file.

And finally, i need to deserialize them and has the ClassA object and
ClassB object from the file.
 
BinaryFormatter.Selrialize(Stream s.....)

just use the same (unclosed) stream hey!

--
Regards,
Lloyd Dupont
NovaMind Software
Mind Mapping at its best
www.nova-mind.com
 
I know how to serialize a object into a file. But if i want to
serialize several different objects into the same file and not at the
same time, Then how can i do it?Thanks
For example:

i need to serialize ClassA object first, then after 1 minute, i need
to serialize ClassB objec to the same file.

And finally, i need to deserialize them and has the ClassA object and
ClassB object from the file.

If you serialize them separately to a memorystream and save the
byte[] to a file, you have a problem that you can't find the
blockstarts back.

So you have to create some sort of index in the file and append a
byte[] which is the result of the serialization of an object. Then when
you deserialize one object, you can find it back inside the file, load
the byte[] and deserialize it. Otherwise you'll run into problems.

FB

--
------------------------------------------------------------------------
Lead developer of LLBLGen Pro, the productive O/R mapper for .NET
LLBLGen Pro website: http://www.llblgen.com
My .NET blog: http://weblogs.asp.net/fbouma
Microsoft MVP (C#)
------------------------------------------------------------------------
 
Can you create an object that wraps objects A and B. So for example you
have an object with properties of type ClassA and ClassB. Then you
serialize your wrapper object. The first time you serielize it, the ClassB
property will be null so the file will only contain object A (plus the
wrapper). The second time you serialize, you assigned the ClassB property
so now both objects will be serialized (plus the wrapper). The downside is
that your wrapper adds a bit of overhead in the file size, but it should be
very little.
 
i can now deserialize serial different object. But i find that if i
deserialize the object by the same application, then it is okey, but
if i write another program in another Project to deserialize the
stored data file that stored the serialized object, then when i try to
deserialize it, it will have the exception: "Unable to find assembly
'Test1, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null", so how
can i ensure that different application can deserialize the stored
data file??Thanks
 
Create a seperate class library project and put your classses in it. Then
when you need to use those class types from any of your applications
reference that assembly.
 
Back
Top