C # writing object to binary

I need to write an object to a binary file. My structure looks like this.

Struct Company { int numberofemployees list of Struct Employee. } Struct Employee { string EmployeeName; string Designation; } 

What is the best way to perform the above operation? Regards Raju

+7
c #
source share
3 answers

What exactly do you want the result to look like? You can write it manually (see Lirik's answer), or if you need runtime support, maybe something like protobuf-net.

It would be trivial to do this if you were to use classes (which I expect you should really be), but additionally protobuf-net v2 (available only as a source at the moment) should work with it "as is".

For information, here is how I will do it as classes:

  public class Company { private readonly List<Employee> employees = new List<Employee>(); public List<Employee> Employees { get { return employees;}} } public class Employee { public string EmployeeName {get;set;} public string Designation {get;set;} } 

This may be decorated with serialization attributes or (again, using protobuf-net v2) something like this test (which passes):

  [Test] public void CanSerializeCompany() { var model = TypeModel.Create(); model.Add(typeof(Company), false).Add("Employees"); model.Add(typeof(Employee), false).Add("EmployeeName", "Designation"); model.CompileInPlace(); Company comp = new Company { Employees = { new Employee { Designation = "Boss", EmployeeName = "Fred"}, new Employee { Designation = "Grunt", EmployeeName = "Jo"}, new Employee { Designation = "Scapegoat", EmployeeName = "Alex"}} }, clone; using(var ms = new MemoryStream()) { model.Serialize(ms, comp); ms.Position = 0; Console.WriteLine("Bytes: " + ms.Length); clone = (Company) model.Deserialize(ms, null, typeof(Company)); } Assert.AreEqual(3, clone.Employees.Count); Assert.AreEqual("Boss", clone.Employees[0].Designation); Assert.AreEqual("Alex", clone.Employees[2].EmployeeName); } 

(and writes 46 bytes)

It should work with private fields, structures, etc. - I need to take a look ...

If you can add attributes, you do not need to configure the model manually (first 4 lines). The rest of the code just shows full round-trip usage.

+7
source share

In my understanding, BinaryFormatter is the tool for this job.

Edit: As Mark explains in the comments, BinaryFormatter has certain flaws. He recommends protobuf-net on his blog.

+9
source share

Here is an example of how you can read / write a binary file:

 using System; using System.IO; public class BinaryFileTest { private static void Main() { FileStream fs = new FileStream("test.dat", FileMode.Create); BinaryWriter w = new BinaryWriter(fs); w.Write(1.2M); w.Write("string"); w.Write("string 2"); w.Write('!'); w.Flush(); w.Close(); fs.Close(); fs = new FileStream("test.dat", FileMode.Open); StreamReader sr = new StreamReader(fs); Console.WriteLine(sr.ReadToEnd()); fs.Position = 0; BinaryReader br = new BinaryReader(fs); Console.WriteLine(br.ReadDecimal()); Console.WriteLine(br.ReadString()); Console.WriteLine(br.ReadString()); Console.WriteLine(br.ReadChar()); fs.Close(); } } 
+5
source share

All Articles