Re: Binary serialization



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!

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

  • Doing the Right Thing with IPersistStream
    ... I have a custom data log file format that I want to turn into a COM ... write the # of members to the stream, then write each member to the ... shadowing it to a disk file for each new section of the datalog, ...
    (microsoft.public.vc.atl)
  • Re: Design question...
    ... All members are static. ... For some reason I'm stuck on the following design question. ... Should this stream be created every time the log needs to be written, or should the stream be a member variable of the logging class and only be opened and closed once per application lifetime. ... This seems like a simple question but for some reason I can't decide which is the right way to do this. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Hand Waving vs. Rigorous Analysis... (was Security Engineeringvs. Crypto Academics...)
    ... > to key management as I understand the term. ... and old members may be revoked. ... working system where those actions could be done in real time. ... stream, and unauthorised ones can. ...
    (sci.crypt)
  • RE: raw data and streams
    ... Ok, I was looking at my file stream for the members, the memory stream allows you to write bytes to the stream already. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Multiple inheritance revisited
    ... Say a base class defines an routine ... > inheritance has no effect if the base classes are forbidden to hold ... having members would eliminate the SI and MI problems, ...
    (comp.object)