Re: Binary serialization



Jacques wrote:

Thanks for the prompt response.

From what I can deduce from your answer below, the answer to my
question is yes? I.e. every unique managed object I create needs its
own streamwriter (and thus streamreader) routine if the desired
result is to store its DATA MEMBERS ONLY! Sorry for my pessimism
about the error prone-ness of the above, but this new (for me)
approach involves significantly more work for me!

Yes then you need your own streamwriter/reader combo. It will be
significantly faster than the binary formatter, so I think it's worth
the try.

FB


Thanks for clearing things up though!
Jacques

"Frans Bouma [C# MVP]" wrote:

Jacques wrote:

Hi

You are correct in assuming that I don't want do save files of 8
bytes! I am writing a realtime data capture app and need to
minimise disk space. Previously I would use a file write routine
that worked similarly to memcpy, ie
"File.Write(&MyClass,sizeof(MyClass))".

My question is this: Is the only way in which I can extract ONLY
the DATA members from the class in question the method of writing
each individual data member to astream/file.. MYSELF? The problem
I have with this is twofold: 1. every custom object needs its
own custom streamwriter routine 2. point nr (1) is quite error
prone for a clumsy programmer like myself!

You can somewhat solve it quite elegantly with the strategy pattern
through some base class/subclass construction: in the base class you
create the stream writer code, in the subclass you override a
method of the base class where you collect the data in to a byte
array which is then written to the stream by the base class. I dont
see how that can go wrong :)

FB


Kind regards
Jacques

"Frans Bouma [C# MVP]" wrote:> Jacques wrote:

Hi
I am an dotNet newby, so pardon my ignorance.

I am looking for a method of saving/copying a managed class
to a stream/file WITHOUT saving the object's state, eg. if I
have a ref class with two int32's as its data members, the
binary file of that class must have a size of 8 bytes (i.e.
only contains class data members, not methods etc.).

Is serialization the answer to the above problem?

No.

Binary serialization is a mechanism which writes that data to
the output stream which is necessary to re-create the object
when reading that precise data. I.o.w.: you get the contents +
extra fluff. If you want to save 8 bytes, you should grab the 8
bytes and write them yourself. Though I fail to see the
necessity for files of 8 bytes. This thus leads to the
assumption that you want to save the data of a lot of objects
into one file. If you do that yourself, you can end up with a
compact file, but also with a lot of code, or better: more code
than you would end up with when you would just go for the
binary formatter approach.

--
------------------------------------------------------------------------
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#)
------------------------------------------------------------------------
.



Relevant Pages

  • Re: Binary serialization
    ... The problem here is that you're working with managed objects. ... Serialization in managed code is that the object can be de-serialized from ... objects's DATA members are the only part of the object I want to store. ... then written to the stream by the base class. ...
    (microsoft.public.dotnet.general)
  • Re: Binary serialization
    ... implement a custom serialisation method for every object I create if the the ... objects's DATA members are the only part of the object I want to store. ... then written to the stream by the base class. ... Is serialization the answer to the above problem? ...
    (microsoft.public.dotnet.general)
  • Re: Binary serialization
    ... DATA members from the class in question the method of writing each ... the base class where you collect the data in to a byte array which is ... then written to the stream by the base class. ...
    (microsoft.public.dotnet.general)
  • Re: Binary serialization
    ... I told you yesterday that you could do this with custom binary ... DATA members from the class in question the method of writing each ... then written to the stream by the base class. ... Is serialization the answer to the above problem? ...
    (microsoft.public.dotnet.general)
  • Re: Fans of Template Method with protected variable?
    ... >>I've recently started using protected variables in the super class instead. ... >>ask when you guys implement this pattern do you do it with private variables ... advocates not having base class data members. ...
    (comp.object)

Loading