By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,985 Members | 1,885 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,985 IT Pros & Developers. It's quick & easy.

C# App: Binary Serialization File Size Allocation

P: 71
hi, I would just like to know if there is a way to manually calculate the size of my structure to anticipate the file size of the object after serialization?

Is it reliable to depend on the file size of the usual result after serializing a certain structure? I mean, if I serialize an object then get the size of the result, will the result on the next serialization will still be the same even I change the data inside the structure?

What I am trying to achieve here is to serialize multiple objects but assign a specific block/position for them inside a file.

I hope I delivered my questions clearly that someone could help me.

Thanks in advance.
Dec 29 '08 #1
Share this Question
Share on Google+
8 Replies

Expert 5K+
P: 7,872
I think it depends on your structure?
If you only use hard sized Types (ints, bytes, etc) then I think it is fine.
But if you have an array ([] or string or Collection type) then I think the size of the serialized object will be different?
Might be better to have a "table" header at the begining of the file that points out where the offsets are?
Dec 29 '08 #2

P: 71
Thanks for the reply.
But I still want to know the exact amount of bytes that my structure can consume.

I was trying to test a specific structure with a single property of int.
Please have a look on the code below.

Expand|Select|Wrap|Line Numbers
  1. public class GameState : ISerializable
  2.     {
  3.         private int inttest;
  4.         public int IntTester
  5.         {
  6.             get { return inttest; }
  7.             set { inttest = value; }
  8.         }
  10.         //private GlobalState _gstate = new GlobalState();
  11.         //private PlayerState _pstate = new PlayerState();
  14.         public GameState() { }
  15.         public GameState(SerializationInfo info, StreamingContext ctxt)
  16.         {
  17.             inttest = (int)info.GetValue("IntTester", typeof(int));
  18.         }
  20.         public void GetObjectData(SerializationInfo info, StreamingContext context)
  21.         {
  22.             info.AddValue("IntTester", inttest);
  23.         }
  24.     }
This structure consumes 151bytes.
If I only have "int" inside that should consume only 4bytes,where did the other remaining bytes came from? Will this 151 bytes be enough to depend on(structure size will always be the same) that it will not change in the future for whatever reason?

Thanks in advance.
Dec 30 '08 #3

Expert 5K+
P: 7,872
Serialization seems to be boggling a lot of minds lately.
You have to remember that serialization is supposed to be bi-directional. You are supposed to get the exact same object back. All the other bytes are data used to describe that exact class type.

Expand|Select|Wrap|Line Numbers
  1. public class myclass1 : ISerializable 
  2.   public int myint=0;
  3. }
  4. public class myclass2 : ISerializable 
  5.    public int myint=0;
  6. }
If it only stored the value of the int, how would it know which class to instanciate in memory when you reloaded it?
Dec 30 '08 #4

P: 71
For that I was thinking that I need to create a separate function that checks the Type of the return object in Deserialization and a function that converts that object into its own type.

Do you think it is a good workaround?
I cant think of other idea,please let me know if you have one or any reaction on that.

I am really stuck into implementing serializing/deserializing any objects so my functions must most of the time dynamic.

Thanks in advance.
Dec 31 '08 #5

Expert 5K+
P: 7,872
Well I am not an expert on serialization, but there are [attributes] that you can apply to your class I think to determine how(and what?) it serializes. Then you use the built in functions to recreate the object?
I don't think you need to do really any work about deciding what class it is, the built in deserialized portion should be able to do it
Dec 31 '08 #6

Expert 100+
P: 190
When you serialize, two things happen:
1. The actual members are written to disk. If these are fixed-size value types, they should occupy the same amount of bytes each time.
2. Meta-data about your class is written to the serialization stream. As Plater said, the meta-data is used to determine the exact source object that needs to be re-created. This includes the assembly name, version and other reflection data. My opinion would be not to rely on that always being the same number of bytes. It probably would be the same if you never recompiled the assembly.

While the serialization attributes allow you to control which members are serialized or not, it will not be as easy to control what meta-data is written to your serialization stream.

Dantz, I think what you want to do is write only the member data to file (for example, your int field) and then on reconstruction, you are simply creating a new object and filling it with the data which was stored to file. Your code takes on the responsibility of knowing what object to create, rather than the serialization. To do so, you might add a "fixed-length" field which indicates the object type.

So you are "emulating" serialization, but you would not be using the built-in serialization libraries of .Net. Nor should you try to "override" the serialization methods. You need to decide if this work-around is what you want, but .Net serialization always needs the meta-data of the object it is dealing with.
Dec 31 '08 #7

Expert 5K+
P: 5,000
Also, don't forget you can write your own serializer and control it how you like.
Dec 31 '08 #8

P: 71
Thank you for your replies and happy new year to all!

That idea sounds good, I think I may give it a try but with limited time I hope I can finish soon. :)

For #1 I tested all of my structures and it all give the same sizes even for strings. So now I just use that size then double it(for safety reasons) to be able to get anticipate the size of my structure.

For #2 Yes that is what i want to do. I will try your suggestion then just get back if there are still problems but I think I achieve what I want based on all your opinions.

Ok, I will check on those [attributes]

Again, thank you for all your replies.
Jan 2 '09 #9

Post your reply

Sign in to post your reply or Sign up for a free account.