Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:

C# ПІДРУЧНИКИ / c# / MS Press - Msdn Training Programming Net Framework With C#

.pdf
Скачиваний:
175
Добавлен:
12.02.2016
Размер:
16.87 Mб
Скачать

Module 12: Serialization

21

 

 

 

Note You can open a binary file using Visual Studio .NET by running Microsoft Windows® Explorer, Explorer.exe, and navigating to the binary file’s icon. Then either right-click on the icon and choose Open With Microsoft Visual Studio .NET, or drag and drop the icon into a running Visual Studio .NET application’s left-hand File View pane.

6.Using Visual Studio .NET, open and visually examine the contents of the Linkedlist.bin file in the bin\Debug subdirectory, and note the serialized list data’s format, structure, and size.

! To create the basic Serialization application in SOAP format

1.Add code to the SaveListToDisk and LoadListFromDisk methods to use the SoapFormatter to write to and read the LinkedList parameter to and from a file named Linkedlist.soap.

2.Build and execute the application, and confirm that the console output remains correct.

3.Using Visual Studio .NET, open and visually examine the contents of the Linkedlist.soap file in the bin\Debug subdirectory, and note the serialized list data’s format and size. Compare this list data’s format, structure, and size to the Linkedlist.bin list data’s format, structure, and size.

22

Module 12: Serialization

Exercise 2

Handling Complex Object Graphs

In this exercise, you will modify the Serialization application to create and manipulate an object graph that has multiple references to the same object and has reference cycles.

!To create the complex Serialization application in SOAP format

1.In Serialize.cs, locate the Ser class and add a static method named SaveArrayToDisk that returns a void and that takes as its single argument an array of type Node. Within SaveArrayToDisk, add code to:

a.Iterate through the array of nodes, and, for each node, print out the array’s index, the node’s value, and the value of the Node object in the node’s NextNode field.

b.Write out a message to the console that states that the application is serializing the array to a file.

c.Create a stream object that is initialized with the file named Array.soap by using the static File.Create method.

d.Create a SoapFormatter object.

e.Serialize the node array to the file.

f.Close the file.

2.In the Ser class, add a static method named LoadArrayFromDisk that takes no arguments and returns an array of type Node. Within

LoadArrayFromDisk, add code to:

a.Write out a message to the console that states that the application is deserializing an array from the file.

b.Create a stream object that is initialized with the file named Array.soap by using the static File.OpenRead method.

c.Create a SoapFormatter object.

d.Using the SoapFormatter object, deserialize the stream into an array of type Node.

e.Close the file.

f.Iterate through the array of nodes, and, for each node, print out the array’s index, the node’s value, and the value of the Node object in the node’s NextNode field.

g.Return the array of type Node.

Module 12: Serialization

23

 

 

 

3.In the Ser class add a static public method named Scope5 that takes no arguments and returns void. Within Scope5, add code to:

a.Create two objects of type Node named n0 and n1 that are initialized to values 0 and 1 respectively.

b.Write out a message to the console that states that the application is entering Scope5 and creating a graph cycle.

c.Assign n1 to the NextNode field of n0.

d.Assign n0 to the NextNode field of n1.

e.Create an array of type Node that is initialized to contain references to the three objects: n0, n1, and n0. Note that index 0 and 2 refer to the same object.

f.Call the SaveArrayToDisk method and pass in the array of type Node.

g.Write out a message to the console that states that the application is leaving Scope5.

4.In the Ser class, add a static public method named Scope6 that takes no arguments and returns void. Within Scope6, add code to:

a.Write out a message to the console that states that the application is entering Scope6 and creating a graph cycle.

b.Assign to a variable named nodes of type array of Node the array that is returned by the LoadArrayFromDisk method. The array references the objects: n0, n1, and n0 that were created in step 1. Note that index 0 and 2 refer to the same object.

c.Write out a message to the console that states that the value of n0 is changing to 42 and that this change should result in a change in the value in both locations in the array.

d.Assign the integer 42 to the Value field of nodes[0].

e.Write out a message to the console that states that the array’s structure should be preserved during serialization and deserialization.

f.Call SaveArrayToDisk to persist nodes to the disk.

g.Assign to a variable named nodes2 of type array of Node the array that is returned by the LoadArrayFromDisk method.

h.Write out a message to the console that states that the value of n0 is being incremented.

i.Increment by 1 the value of the first element of nodes2.

j.Call SaveArrayToDisk, and pass nodes2.

k.Write out a message to the console that states that the application is leaving Scope6.

5.In the Main method of Ser, and after the call to the Scope4 method, add calls to Scope5 and Scope6.

6.Build the Serialization application.

7.Step through the application in the Visual Studio .NET debugger, and note console output similar to the following output:

24

Module 12: Serialization

Entering Scope 1

Creating and filling List ..

List: 1 2 3 4 5 6 7 8 9

Serializing LinkedList to file ..

Leaving Scope 1

Entering Scope 2

Deserializing LinkedList from soap file .. Deserializing LinkedList from binary file .. List: 1 2 3 4 5 6 7 8 9

Swapping Entries

Swapping 1 and 2

Swapping 3 and 4

Swapping 5 and 6

Swapping 7 and 8

List: 2 1 4 3 6 5 8 7 9 Serializing LinkedList to file .. Leaving Scope 2

Entering Scope 3

Deserializing LinkedList from soap file .. Deserializing LinkedList from binary file .. List: 2 1 4 3 6 5 8 7 9

Swapping Random Entries Swapping 8 and 7 Swapping 7 and 5 Swapping 6 and 5 Swapping 2 and 1 Swapping 1 and 8 Swapping 4 and 2 Swapping 2 and 3 Swapping 3 and 5 Swapping 9 and 4 Swapping 9 and 4 Swapping 6 and 8 Swapping 2 and 4 Swapping 2 and 1 Swapping 9 and 7 Swapping 1 and 5

List: 6 5 1 4 3 9 8 2 7 Serializing LinkedList to file .. Leaving Scope 3

Entering Scope 4

Deserializing LinkedList from soap file ..

Deserializing

LinkedList from

binary file ..

List: 6

5

1

4

3

9

8

2

7

Removing

Entries

 

 

 

 

 

Removing

1

 

 

 

 

 

 

 

Removing

2

 

 

 

 

 

 

 

Removing

3

 

 

 

 

 

 

 

List: 6

5

4

9

8

7

 

 

 

Serializing LinkedList to file ..

Leaving Scope

4

 

 

 

 

 

(Code continued on the following page.)

Module 12: Serialization

25

 

 

 

Entering Scope 5

Creating a circular reference:

n0's NextNode is n1 and n1's NextNode is n0

Also adding in a third node, n2, that is another reference to n0

Node 0 Value: 0 Value of NextNode Ref: 1

Node 1 Value: 1 Value of NextNode Ref: 0

Node 2 Value: 0 Value of NextNode Ref: 1

Serializing Array to file ..

Leaving Scope 5

Entering Scope 6

Deserializing Array from file ..

Node 0 Value: 0 Value of NextNode Ref: 1

Node 1 Value: 1 Value of NextNode Ref: 0

Node 2 Value: 0 Value of NextNode Ref: 1

changing value of node n0 to 42

should change value in both locations in array

array's structure should be preserved during serialization and deserialization

Node 0 Value: 42 Value of NextNode Ref: 1

Node 1 Value: 1 Value of NextNode Ref: 42

Node 2 Value: 42 Value of NextNode Ref: 1

Serializing Array to file ..

Deserializing Array from file ..

Node 0 Value: 42 Value of NextNode Ref: 1

Node 1 Value: 1 Value of NextNode Ref: 42

Node 2 Value: 42 Value of NextNode Ref: 1

incrementing value of node n0

Node 0 Value: 43 Value of NextNode Ref: 1

Node 1 Value: 1 Value of NextNode Ref: 43

Node 2 Value: 43 Value of NextNode Ref: 1

Serializing Array to file ..

Leaving Scope 6

26Module 12: Serialization

8.Using Visual Studio .NET, open and visually examine the Array.soap file in the bin\Debug subdirectory, and note the serialized array data’s format, structure, and size.

Module 12: Serialization

27

 

 

 

Review

Topic Objective

To reinforce module objectives by reviewing key points.

Lead-in

The review questions cover some of the key concepts taught in the module.

!Serialization Scenarios

!Serialization Attributes

!Object Graph

!Serialization Process

!Serialization Example

!Deserialization Example

!Custom Serialization

!Custom Serialization Example

!Security Issues

*****************************ILLEGAL FOR NON-TRAINER USE******************************

1.Declare a serializable class named Foo with two integer fields F1 and F2 in which F2 is transient and should not be serialized.

[Serializable] public class Foo { int F1;

[NonSerialized] int F2;

2.Name and describe the two kinds of formatters that the .NET Framework provides.

BinaryFormatter for a compact binary SoapFormatter for XML representation

3.Describe what a class should do to provide custom serialization.

The class should inherit from the ISerializable interface, implement the interface’s GetObjectData method, and provide a constructor that takes SerializationInfo and StreamingContext parameters.

4.What kind of data that is not normally accessible by clients can be made visible by serialization?

Private object state

Module 13: Remoting

and XML Web Services

Contents

 

Overview

1

Remoting

2

Remoting Configuration Files

19

Demonstration: Remoting

22

Lab 13.1: Building an Order-Processing

 

Application by Using Remoted Servers

28

XML Web Services

36

Lab 13.2: Using an XML Web Service

48

Review

54

Course Evaluation

56

Information in this document, including URL and other Internet Web site references, is subject to change without notice. Unless otherwise noted, the example companies, organizations, products, domain names, e-mail addresses, logos, people, places and events depicted herein are fictitious, and no association with any real company, organization, product, domain name, e-mail address, logo, person, place or event is intended or should be inferred. Complying with all applicable copyright laws is the responsibility of the user. Without limiting the rights under copyright, no part of this document may be reproduced, stored in or introduced into a retrieval system, or transmitted in any form or by any means (electronic, mechanical, photocopying, recording, or otherwise), or for any purpose, without the express written permission of Microsoft Corporation.

Microsoft may have patents, patent applications, trademarks, copyrights, or other intellectual property rights covering subject matter in this document. Except as expressly provided in any written license agreement from Microsoft, the furnishing of this document does not give you any license to these patents, trademarks, copyrights, or other intellectual property.

2001-2002 Microsoft Corporation. All rights reserved.

Microsoft, ActiveX, BizTalk, IntelliMirror, Jscript, MSDN, MS-DOS, MSN, PowerPoint, Visual Basic, Visual C++, Visual C#, Visual Studio, Win32, Windows, Windows Media, and

Window NT are either registered trademarks or trademarks of Microsoft Corporation in the U.S.A. and/or other countries.

The names of actual companies and products mentioned herein may be the trademarks of their respective owners.

Соседние файлы в папке c#