Description = 'Wrox.com Web site',

  NextURL = null

 };

 //---link the first BookMark to the next---

 bm1.NextURL = bm2;

 //========Binary Serialization=========

 //---serializing an object graph into a memory stream---

 MemoryStream ms = Serialize(bm1);

}

To prove that the object is serialized correctly, you deserialize the memory stream (that is, 'unflatten' the data) and assign it back to a BookMark object:

static void Main(string[] args) {

 BookMark bm1, bm2;

 bm1 = new BookMark {

  URL = 'http://www.amazon.com',

  Description = 'Amazon.com Web site'

 };

 bm2 = new BookMark() {

  URL = 'http://www.wrox.com',

  Description = 'Wrox.com Web site',

  NextURL = null

 };

 //---link the first BookMark to the next---

 bm1.NextURL = bm2;

 //========Binary Serialization=========

 //---serializing an object graph into a memory stream---

 MemoryStream ms = Serialize(bm1);

 //---deserializing a memory stream into an object graph---

 BookMark bm3 = Deserialize(ms);

}

Here is the definition for the DeSerialize() function:

static void Main(string[] args) {

 //...

}

static MemoryStream Serialize(BookMark bookMark) {

 //...

}

static BookMark Deserialize(MemoryStream ms) {

 BinaryFormatter formatter = new BinaryFormatter();

 return (BookMark)formatter.Deserialize(ms);

}

To display the values of the deserialized BookMark object, you can print out them out like this:

static void Main(string[] args) {

 BookMark bm1, bm2;

 bm1 = new BookMark {

  URL = 'http://www.amazon.com',

  Description = 'Amazon.com Web site'

 };

 bm2 = new BookMark() {

  URL = 'http://www.wrox.com',

  Description = 'Wrox.com Web site',

  NextURL = null

 };

 //---link the first BookMark to the next---

 bm1.NextURL = bm2;

 //========Binary Serialization=========

 //---serializing an object graph into a memory stream---

 MemoryStream ms = Serialize(bm1);

}

To prove that the object is serialized correctly, you deserialize the memory stream (that is, 'unflatten' the data) and assign it back to a BookMark object:

static void Main(string[] args) {

 BookMark bm1, bm2;

 bm1 = new BookMark {

  URL = 'http://www.amazon.com',

  Description = 'Amazon.com Web site'

 };

 bm2 = new BookMark() {

  URL = 'http://www.wrox.com',

  Description = 'Wrox.com Web site',

  NextURL = null

 };

 //---link the first BookMark to the next---

 bm1.NextURL = bm2;

 //========Binary Serialization=========

 //---serializing an object graph into a memory stream---

 MemoryStream ms = Serialize(bm1);

 //---deserializing a memory stream into an object graph---

 BookMark bm3 = Deserialize(ms);

 //---print out all the bookmarks---

 BookMark tempBookMark = bm3;

 do {

  Console.WriteLine(tempBookMark.URL);

  Console.WriteLine(tempBookMark.Description);

  Console.WriteLine(tempBookMark.GetDateCreated());

  Console.WriteLine(' ');

  tempBookMark = tempBookMark.NextURL;

 } while (tempBookMark != null);

 Console.ReadLine();

}

If the objects are serialized and deserialized correctly, the output is as shown in Figure 11-12.

Figure 11-12

But what does the binary stream look like? To answer that question, take a look at the c: BookMarks.dat file that you have created in the process. To view the binary file, simply drag and drop the BookMarks.dat file into Visual Studio 2008. You should see something similar to Figure 11-13.

Figure 11-13

A few observations are worth noting at this point:

Вы читаете C# 2008 Programmer's Reference
Добавить отзыв
ВСЕ ОТЗЫВЫ О КНИГЕ В ИЗБРАННОЕ

0

Вы можете отметить интересные вам фрагменты текста, которые будут доступны по уникальной ссылке в адресной строке браузера.

Отметить Добавить цитату