Xem mẫu

2.2 Streams 37 _purchaseOrderStatus=purchaseOrderStates.PAID _invoiceDate=DateTime.Now end if end sub End Class Note: The use of the [Serializable()] tag facilitates deep seilalization. It is possible to perform deep serialization without this tag by using surrogates. A surrogate is where the a class implements ISerializationSurrogate, and is passed to the AddSurrogate method of a SurrogateSelector object. The SurrogateSelector property of the formatter is then set equal to this object prior to serialization. The _purchaseOrderStatus variable is private and can only be modified by recordDelivery(), recordInvoice(), and recordPayment(). This ensures that a bug elsewhere in the code will not cause undelivered goods to be paid for (i.e., _purchaseOrderStatus cannot change directly from ISSUED to PAID). Similarly, the date recording is encapsulated within the object and cannot be externally manipulated. To place a purchase order on a stream (either to disk or to the network), you could write each value one after the other as text, separated by commas, and have the receiver parse out the values and re-create the object; however, there is an easier way: serialization. To write the object to a stream and save the object to disk, you could use the following code: C# private void button1_Click(object sender, System.EventArgs e) { company Vendor = new company(); company Buyer = new company(); lineItem Goods = new lineItem(); purchaseOrder po = new purchaseOrder(); Vendor.name = "Acme Inc."; Buyer.name = "Wiley E. Coyote"; Goods.description = "anti-RoadRunner cannon"; Goods.quantity = 1; Goods.cost = 599.99; Chapter 2 38 2.2 Streams po.items = new lineItem[1]; po.items[0] = Goods; po.buyer = Buyer; po.vendor = Vendor; SoapFormatter sf = new SoapFormatter(); FileStream fs = File.Create("C:\\po.xml"); sf.Serialize(fs,po); fs.Close(); } VB.NET Private Sub Button1_Click(ByVal sender As Object, ByVal e As _ System.EventArgs) Handles Button1.Click Dim Vendor As company = New company() Dim Buyer As company = New company() Dim Goods As lineItem = New lineItem() Dim po As purchaseOrder = New purchaseOrder() Vendor.name = "Acme Inc." Buyer.name = "Wiley E. Coyote" Goods.description = "anti-RoadRunner cannon" Goods.quantity = 1 Goods.cost = 599.99 po.items = New lineItem(1) {} po.items(0) = Goods po.buyer = Buyer po.vendor = Vendor Dim sf As SoapFormatter = New SoapFormatter() Dim fs As FileStream = File.Create("C:\po.xml") sf.Serialize(fs,po) fs.Close() End Sub To read the object back into memory, we can deserialize it thus: C# private void button2_Click(object sender, System.EventArgs e) { SoapFormatter sf = new SoapFormatter(); 2.2 Streams 39 FileStream fs = File.OpenRead("C:\\po.xml"); purchaseOrder po = (purchaseOrder)sf.Deserialize(fs); fs.Close(); MessageBox.Show("Customer is " + po.buyer.name); } VB.NET Private Sub button2_Click(ByVal sender As Object, ByVal e As_ System.EventArgs) Handles Button2.Click Dim sf As SoapFormatter = New SoapFormatter() Dim fs As FileStream = File.OpenRead("C:\po.xml") Dim po As purchaseOrder = CType(sf.Deserialize(fs),_ purchaseOrder) fs.Close() MessageBox.Show("Customer is " + po.buyer.name) End Sub Before this code will work, you will need an assembly reference for SoapFormatter.This is done by clicking Project®Add Reference and select-ing System.Runtime.Serialization.Formatters.Soap, then adding this line to the top of the code: C# using System.IO; using System.Runtime.Serialization.Formatters.Soap; VB.NET imports System.IO imports System.Runtime.Serialization.Formatters.Soap To test this application, run it from Visual Studio .NET. Press the Serial-ize button and then the Deserialize button. You will see the message “Cus-tomer is Wiley E. Coyote,” as depicted in Figure 2.3. If you open the file C:\PO.XML, you will see a human-readable represen-tation of the object, as shown in Figure 2.4. This format is known as simple object access protocol (SOAP) and is very portable between platforms (e.g., WebSphere for UNIX can read it). Chapter 2 40 2.2 Streams Figure 2.3 Serializing .NET classes. Note: The constructor is not called during deserialization. In the above example, you will see that the issue date does not change when the object is re-created from disk. The significant methods and properties for SoapFormatter are shown in Table 2.4. Figure 2.4 XML view of a serialized object. 2.2 Streams 41 Table 2.4 Significant members of SoapFormatter . Method or Property Constructor Deserialize Serialize AssemblyFormat TypeFormat TopObject Purpose Initializes a new instance of the SoapFormatter class. It may be invoked without any parameters. Deserializes a stream into an object graph. It may be invoked thus: Deserialize(Stream). Serializes an object or graph of connected objects. It may be invoked thus: Serialize(Stream, object). Gets or sets the format in which assembly names are serialized. Returns FormatterAssemblyStyle. Gets or sets the format in which type descriptions are laid out in the serialized stream. Returns FormatterTypeStyle. Gets or sets the ISoapMessage into which the SOAP top object is deserialized. Returns ISoapMessage. Serializing to binary SOAP formatting may be very impressive, but it is far from compact and may be quite bandwidth consuming if sent over a slow network. We can therefore use the native binary format to store the array by substituting SoapFormatter with BinaryFormatter in the above example thus: C# BinaryFormatter bf = new BinaryFormatter(); FileStream fs = File.Create("C:\\po.bin"); bf.Serialize(fs,po); fs.Close(); VB.NET Dim bf As BinaryFormatter = New BinaryFormatter() Dim fs As FileStream = File.Create("C:\po.bin") bf.Serialize(fs,po) fs.Close() And deserialize with this code: C# BinaryFormatter bf = new BinaryFormatter(); FileStream fs = File.OpenRead("C:\\po.bin"); Chapter 2 ... - slideshare.vn
nguon tai.lieu . vn