2010-04-08 99 views
7

我必須將對象寫入二進制文件中。我的結構如下所示。C#將對象寫入二進制文件

Struct Company 
    { 
     int numberofemployees 
     list of Struct Employee. 
    } 

    Struct Employee 
    { 
     string EmployeeName; 
     string Designation; 
    } 

做上述操作的最佳方法是什麼? Registers Raju

+4

爲什麼這是一個結構?這些看起來不像'struct'的好用處,我希望這會以有趣的方式咬你。我會在這裏使用'class' ... – 2010-04-08 11:51:32

回答

7

究竟你所要的輸出是什麼樣子?你可以手動編寫它(請參閱Lirik的答案),或者如果你想運行時支持,可能類似protobuf-net。

如果你正在使用類(我希望你實際上應該是這樣),那麼這將是微不足道的,但另外protobuf-net v2(僅適用於此時的源代碼)應該與「按原樣」一起工作。

有關的信息,這裏是我會怎麼做它作爲類:

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;} 
    } 

這可能與序列化的屬性來裝飾,或(再次使用protobuf網V2)這樣的測試(其中通過) :

[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); 
    } 

(並寫入46個字節)

應該與私人領域,結構等工作 - 我必須採取一個廁所k ...

如果您可以添加屬性,則不需要手動設置模型(前4行)。其餘代碼僅顯示完整的往返使用情況。

9

以我的理解,BinaryFormatter是此工作的工具。

編輯:正如Marc在評論中所說的,BinaryFormatter有一些缺點。他在他的博客中建議protobuf-net

+0

+1 Jens,看起來比簡單地寫文件來得好。 – Kiril 2010-04-08 10:05:45

+0

我試過使用二進制格式化程序。在將第一個對象寫入文件之前,它將添加一些標題。我不想要任何標題數據。 – user209293 2010-04-08 10:12:10

+1

是的,它給你一個戰鬥機會,有一天能夠正確地讀取數據。 – 2010-04-08 11:03:13

5

Here is an example你如何讀/寫的二進制文件:

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(); 

    } 
}