I have to write an object in to binary file.My struct looks like this.
Struct Company
{
int numberofemployees
list of Struct Employee.
}
Struct Employee
{
string EmployeeName;
string Designation;
}
What is the best way to do the above operation?
Regards
Raju
What exactly do you want the output to look like? You can write it manually (see Lirik’s answer), or if you want runtime support, perhaps something like protobuf-net.
This would be trivial to do if you were using classes (which I expect you actually should be), but additionally protobuf-net v2 (only available as source at the moment) should work with that “as is”.
For info, here is how I would do it as classes:
This could be decorated with serialization attributes, or (again, using protobuf-net v2) something like this test (which passes):
(and writes 46 bytes)
It should work with private fields, structs, etc – I’d have to take a look…
If you are able to add attributes, then you don’t need to set up the model manually (the first 4 lines). The rest of the code is just showing full round-trip usage.