c# - Random access and BinaryWriter? -


I am working on a project where I am creating a byte array which is almost like the order of the object graph is. Each class is responsible for writing their data, so I'm thinking of using a binarywriter and I pass that object to my children's objects and make them temporary buffers (byte arrays) and make them Rather than merging them, they are serialized in themselves.

However, I have to fill each childhood with a byte which specifies the size of the lower block, but unless the child has not written all the data, then it is not known, and at that time As far as the author is located at that place, the end of the childbender I can definitely make a temporary buffer and the child can pass a temporary binderer and after this I want to write the data to the "main" binary writer, Ekin it seems like a Hashis solution.

Is there any alternative solution? Is it possibly a bad idea to tamper with the situation of binarywriter? I would probably like to avoid creating temporary buffers / writers if possible, and just write each child on their "main author" / buffer

, the solutions given below can work, but little Are there any good solutions? (I have not tested it, so the byte is not aligned)

  Public abstract class base object {public abstract zero serialize (binarywriter writer); } Public Sector Program () {Public Fixed Zero Main () {List & lt; Base object & gt; MyChildren = new list & lt; BaseObject & gt; (); // ... Start kids here .. MemoryStream memoryStream = new MemoryStream (); BinaryWriter author = new BinaryWriter (memorystream); Exotic Hair (Base Baby Child In My Child) {Authors Write (byte) 0); // dummy placeholder length is a byte long size position = authors. Basestream composition; Child.Serialize (author); Long Current Position = Writer Basestream composition; Author. Peak (shape location, SeekOrigin.Begin); Author.Write ((byte) currentPosition-sizePosition); Author. Peak (current situation, SeekOrigin.Begin); I too was stumped with a question too recently and it also realized that it is practically a binary writer practically associated with a special stream because as soon as you have a binary writer (according to the document) Turn off, the stream also stops, I have come up with a better solution than those who tell you: 

  1. Write the data in the memory stream, then the memory stream size and content Write in the main stream. It can be easy if you want to use some objects other than a binarywriter in addition to another type of author.
  2. Remember your current status, type 0 value, then the data block, after this, take back the value 0 and replace it with the written size, then return to the end.

I chose the option of No.1. Although I have not completed my implementation yet, knowing what size is being written before writing something, it seems to me better. It sounds more suited to write dummy value and jump around the stream. And it's probably more similar to how BinaryWriter writes the wire internally, do you think, or have you considered being binding from BinaryWriter for the job of writing yourself?


Comments

Popular posts from this blog

oracle - The fastest way to check if some records in a database table? -

php - multilevel menu with multilevel array -

jQuery UI: Datepicker month format -