almo2001

Formatter Services example

Jun 18th, 2015
32
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.04 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Reflection;
  6. using System.Runtime.Serialization;
  7. using System.Runtime.Serialization.Formatters.Binary;
  8. using System.Text;
  9. using System.Threading.Tasks;
  10.  
  11. namespace FormatterServicesTest
  12. {
  13.     class Program
  14.     {
  15.         static void Main(string[] args)
  16.         {
  17.             TestLibrary();
  18.             Console.WriteLine("Press enter to close...");
  19.             Console.ReadLine();
  20.         }
  21.  
  22.         static void TestLibrary()
  23.         {
  24.             Library theLib = new Library();
  25.  
  26.             Book aBook = new Book("HHGttG", "Douglas Adams", "Good book");
  27.             theLib.AddBook(aBook);
  28.  
  29.             aBook = new Book("Room", "Emily", "Good book");
  30.             theLib.AddBook(aBook);
  31.  
  32.             aBook = new Book("Raft", "Stephen Baxter", "First book");
  33.             theLib.AddAudioBook(aBook);
  34.  
  35.             theLib.Dump();
  36.         }
  37.  
  38.         static void TestBook()
  39.         {
  40.             Book aBook = new Book("HHGttG", "Douglas Adams", "Good book");
  41.             Console.WriteLine("-----------");
  42.             aBook.Dump(0);
  43.  
  44.             string path = Path.GetTempFileName();
  45.             var formatter = new BinaryFormatter();
  46.             using (var fileStream = new FileStream(path, FileMode.Create))
  47.             {
  48.                 formatter.Serialize(fileStream, aBook);
  49.             }
  50.  
  51.             aBook.Title = "Dirk Gently's";
  52.             aBook.Comment = "Ok book";
  53.             Console.WriteLine("-----------");
  54.             aBook.Dump(0);
  55.  
  56.             Book tempBook;
  57.             using (var fileStream = new FileStream(path, FileMode.Open, FileAccess.Read))
  58.             {
  59.                 tempBook = (Book)formatter.Deserialize(fileStream);
  60.             }
  61.  
  62.             MemberInfo[] members = FormatterServices.GetSerializableMembers(typeof(Book));
  63.             FormatterServices.PopulateObjectMembers(aBook, members, FormatterServices.GetObjectData(tempBook, members));
  64.  
  65.             Console.WriteLine("-----------");
  66.             aBook.Dump(0);
  67.         }
  68.     }
  69.  
  70.     [Serializable]
  71.     class Book
  72.     {
  73.         public string Title;
  74.         public string Author;
  75.         [NonSerialized]
  76.         public string Comment;
  77.  
  78.         // Constructor for setting new values.
  79.         public Book(string newTitle, string newAuthor, string newComment)
  80.         {
  81.             Title = newTitle;
  82.             Author = newAuthor;
  83.             Comment = newComment;
  84.         }
  85.  
  86.         public void Dump(int indent)
  87.         {
  88.             string indentStr = "";
  89.             for (int i = 0; i < indent; i++)
  90.             {
  91.                 indentStr += " ";
  92.             }
  93.             Console.WriteLine("{0}Title: {1}", indentStr, this.Title);
  94.             Console.WriteLine("{0}Author: {1}", indentStr, this.Author);
  95.             Console.WriteLine("{0}Comment: {1}", indentStr, this.Comment);
  96.         }
  97.     }
  98.  
  99.     [Serializable]
  100.     class Library
  101.     {
  102.         public Dictionary<int, Book> Books = new Dictionary<int, Book>();
  103.         public Dictionary<int, Book> AudioBooks = new Dictionary<int, Book>();
  104.  
  105.         public Library()
  106.         {
  107.         }
  108.  
  109.         public void AddBook(Book newBook)
  110.         {
  111.             Books.Add(Books.Count, newBook);
  112.         }
  113.  
  114.         public void AddAudioBook(Book newBook)
  115.         {
  116.             AudioBooks.Add(Books.Count, newBook);
  117.         }
  118.  
  119.         public void Dump()
  120.         {
  121.             Console.WriteLine("---------\nBooks:");
  122.             bool first = true;
  123.             foreach (Book book in Books.Values)
  124.             {
  125.                 if (!first)
  126.                 {
  127.                     Console.WriteLine();
  128.                 }
  129.                 else
  130.                 {
  131.                     first = false;
  132.                 }
  133.                 book.Dump(4);
  134.             }
  135.             Console.WriteLine("---------\nAudio books:");
  136.             foreach (Book book in AudioBooks.Values)
  137.             {
  138.                 book.Dump(4);
  139.             }
  140.  
  141.             Console.WriteLine();
  142.         }
  143.     }
  144.  
  145. }
Advertisement
Add Comment
Please, Sign In to add comment