Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Linq;
- using System.Reflection;
- using System.Runtime.Serialization;
- using System.Runtime.Serialization.Formatters.Binary;
- using System.Text;
- using System.Threading.Tasks;
- namespace FormatterServicesTest
- {
- class Program
- {
- static void Main(string[] args)
- {
- TestLibrary();
- Console.WriteLine("Press enter to close...");
- Console.ReadLine();
- }
- static void TestLibrary()
- {
- Library theLib = new Library();
- Book aBook = new Book("HHGttG", "Douglas Adams", "Good book");
- theLib.AddBook(aBook);
- aBook = new Book("Room", "Emily", "Good book");
- theLib.AddBook(aBook);
- aBook = new Book("Raft", "Stephen Baxter", "First book");
- theLib.AddAudioBook(aBook);
- theLib.Dump();
- }
- static void TestBook()
- {
- Book aBook = new Book("HHGttG", "Douglas Adams", "Good book");
- Console.WriteLine("-----------");
- aBook.Dump(0);
- string path = Path.GetTempFileName();
- var formatter = new BinaryFormatter();
- using (var fileStream = new FileStream(path, FileMode.Create))
- {
- formatter.Serialize(fileStream, aBook);
- }
- aBook.Title = "Dirk Gently's";
- aBook.Comment = "Ok book";
- Console.WriteLine("-----------");
- aBook.Dump(0);
- Book tempBook;
- using (var fileStream = new FileStream(path, FileMode.Open, FileAccess.Read))
- {
- tempBook = (Book)formatter.Deserialize(fileStream);
- }
- MemberInfo[] members = FormatterServices.GetSerializableMembers(typeof(Book));
- FormatterServices.PopulateObjectMembers(aBook, members, FormatterServices.GetObjectData(tempBook, members));
- Console.WriteLine("-----------");
- aBook.Dump(0);
- }
- }
- [Serializable]
- class Book
- {
- public string Title;
- public string Author;
- [NonSerialized]
- public string Comment;
- // Constructor for setting new values.
- public Book(string newTitle, string newAuthor, string newComment)
- {
- Title = newTitle;
- Author = newAuthor;
- Comment = newComment;
- }
- public void Dump(int indent)
- {
- string indentStr = "";
- for (int i = 0; i < indent; i++)
- {
- indentStr += " ";
- }
- Console.WriteLine("{0}Title: {1}", indentStr, this.Title);
- Console.WriteLine("{0}Author: {1}", indentStr, this.Author);
- Console.WriteLine("{0}Comment: {1}", indentStr, this.Comment);
- }
- }
- [Serializable]
- class Library
- {
- public Dictionary<int, Book> Books = new Dictionary<int, Book>();
- public Dictionary<int, Book> AudioBooks = new Dictionary<int, Book>();
- public Library()
- {
- }
- public void AddBook(Book newBook)
- {
- Books.Add(Books.Count, newBook);
- }
- public void AddAudioBook(Book newBook)
- {
- AudioBooks.Add(Books.Count, newBook);
- }
- public void Dump()
- {
- Console.WriteLine("---------\nBooks:");
- bool first = true;
- foreach (Book book in Books.Values)
- {
- if (!first)
- {
- Console.WriteLine();
- }
- else
- {
- first = false;
- }
- book.Dump(4);
- }
- Console.WriteLine("---------\nAudio books:");
- foreach (Book book in AudioBooks.Values)
- {
- book.Dump(4);
- }
- Console.WriteLine();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment