Csharp/CSharp Tutorial/File Directory Stream/Binary Formatter — различия между версиями

Материал из .Net Framework эксперт
Перейти к: навигация, поиск
м (1 версия)
 
(нет различий)

Текущая версия на 15:20, 26 мая 2010

Use BinaryFormatter to Serialize an object to file

<source lang="csharp">using System; using System.IO; using System.Runtime.Serialization; using System.Runtime.Serialization.Formatters.Binary; [Serializable] class Employee {

 private int Number;
 private string FirstName;
 private string LastName;
 public void WriteEmployee(){
   Console.WriteLine("Number: " + this.Number);
   Console.WriteLine("FirstName: " + this.FirstName);
   Console.WriteLine("LastName: " + this.LastName);
 }
 public Employee(int newNumber, string newFirstName, string LastName){
   this.Number = newNumber;
   this.FirstName = newFirstName;
   this.LastName = LastName;
 }

} class MainClass {

 public static void Main() 
 {
   Employee emp = new Employee(1, "X", "F");
   emp.WriteEmployee();
   FileStream serializeStream = new FileStream("c:\\test.dat", FileMode.Create);
   BinaryFormatter bf = new BinaryFormatter();
   bf.Serialize(serializeStream, emp);
   serializeStream.Flush();
   serializeStream.Close();
   FileStream retrieveStream = new FileStream("c:\\test.dat", FileMode.Open);
   Employee NewEmployee = (Employee) bf.Deserialize(retrieveStream);
   NewEmployee.WriteEmployee();
 }

}</source>

Number: 1
FirstName: X
LastName: F
Number: 1
FirstName: X
LastName: F

Use BinaryFormatter to serialize user defined class

<source lang="csharp">using System; using System.Collections.Generic; using System.Text; using System.IO; using System.Runtime.Serialization; using System.Runtime.Serialization.Formatters.Binary; [Serializable] public class Product {

   public long Id;
   public string Name;
   public double Price;
   [NonSerialized]
   string Notes;
   public Product(long id, string name, double price, string notes) {
       Id = id;
       Name = name;
       Price = price;
       Notes = notes;
   }
   public override string ToString() {
       return string.Format("{0}: {1} (${2:F2}) {3}", Id, Name, Price, Notes);
   }

} class Program {

   static void Main(string[] args) {
           List<Product> products = new List<Product>();
           products.Add(new Product(1, "A", 1000.0, "s"));
           products.Add(new Product(2, "B", 25.0, "T"));
           products.Add(new Product(4, "C", 12.0, "k"));
           foreach (Product product in products) {
               Console.WriteLine(product);
           }
           IFormatter serializer = new BinaryFormatter();
           FileStream saveFile = new FileStream("Products.bin", FileMode.Create, FileAccess.Write);
           serializer.Serialize(saveFile, products);
           saveFile.Close();
           FileStream loadFile =new FileStream("Products.bin", FileMode.Open, FileAccess.Read);
           List<Product> savedProducts = serializer.Deserialize(loadFile) as List<Product>;
           loadFile.Close();
           foreach (Product product in savedProducts) {
               Console.WriteLine(product);
           }
   }

}</source>