按键盘上方向键 ← 或 → 可快速上下翻页,按键盘上的 Enter 键可回到本书目录页,按键盘上方向键 ↑ 可回到本页顶部!
————未阅读完?加入书签已便下次继续阅读!
End Set
End Property
Public Property DrawDate() As DateTime
Get
Return _drawDate
End Get
Set(ByVal value As DateTime)
_drawDate = value
End Set
End Property
Public Property Numbers() As Integer()
Get
Return _numbers
End Get
Set(ByVal value As Integer())
_numbers = value
End Set
End Property
Private _bonus As Integer
Private _drawDate As DateTime
Private _numbers As Integer()
End Class
Ticket is plain…vanilla data class。 However; the bolded parts deserve an explanation。 The
first bolded part is a set of angled brackets surrounding the identifier Serializable; which
represents a attribute。 In ; you have the ability to describe types; methods; and so on。
…………………………………………………………Page 296……………………………………………………………
274 CH AP T E R 1 0 ■ L E A R N I N G A B OU T P E R S IS TE N CE
Attributes are descriptions that are used in a particular context。 As opposed to the identifiers
Public and MustInherit; which are descriptions that fundamentally describe how a type will
behave; the Serializable attribute describes the behavior of a type in a specific context—when
the object is to be converted from memory to a data stream and vice versa。 Fundamental descrip
tions are important to the runtime; whereas attributes are generally not important
to the runtime; but important to libraries that the runtime will execute。
■Note Serialization is the process of turning an object into a format that can be saved or sent over the
network。 Deserialization is the process of retrieving the serialized object。
The Serializable attribute describes the ability to serialize the type as it is declared。 When
Ticket is converted into a binary stream; the programmer does not need to do anything other
than pass an instance to the data stream。 The data stream libraries handle all of the other details。
In the declaration of Ticket; the parameterless constructor has been bolded to emphasize
that this type of constructor is necessary when converting a data stream into an object instance。
When binary streams restore types; they instantiate an empty object and then assign the data
members。 Thus; when an object is created; a parameterless constructor is needed。
Converting a Text Stream into a Binary Stream
To convert the stream from text to binary involves breaking apart the text stream; instantiating
a Ticket instance; assigning the data members; and saving the instance to the binary stream。
All of these steps are performed in the following source code (you’ll need a reference to
LottoLibrary in Text2Binary)。
Imports System。IO
Imports System。Runtime。Serialization。Formatters。Binary
Imports System。Text
Public Class LottoTicketProcessor : Implements IText2BinaryProcessor
Public Sub Process(ByVal reader As TextReader; ByVal writer As Stream) _
Implements IText2BinaryProcessor。Process
Dim retval As StringBuilder = New StringBuilder()
Do While reader。Peek() …1
Dim lineOfText As String = reader。ReadLine()
Dim splitUpText As String()= lineOfText。Split(New Char() {〃 〃c})
Dim dateSplit As String() = splitUpText(0)。Split(New Char() {〃。〃c})
Dim ticket As LottoLibrary。Ticket = _
New LottoLibrary。Ticket( _
New DateTime( _
Integer。Parse(dateSplit(0)); _
Integer。Parse(dateSplit(1)); _
Integer。Parse(dateSplit(2))); _
…………………………………………………………Page 297……………………………………………………………
CH A PT E R 1 0 ■ L E A R N I N G A B O U T P E R S IS T E N CE 275
New Integer() { _
Integer。Parse(splitUpText(1)); _
Integer。Parse(splitUpText(2)); _
Integer。Parse(splitUpText(3)); _
Integer。Parse(splitUpText(4)); _
Integer。Parse(splitUpText(5)); _
Integer。Parse(splitUpText(6)) }; _
Integer。Parse(splitUpText(7)))
Dim formatter As BinaryFormatter = New BinaryFormatter()
formatter。Serialize(writer; ticket)
Loop
End Sub
End Class
The code splits the text stream by reading a line of text and then splitting apart the fields。
The split…apart fields are then converted into numbers by using the Integer。Parse() method。
This process of splitting and conversion is called marshaling data。 Marshaling is a technical
term that means to convert a type from one medium to another。
We manage the text marshaling; but manages the binary marshaling; which is still there
behind the scenes。 The System。Runtime。Serialization。Formatters。Binary。BinaryFormatter type
manages marshaling of the Ticket instance to the binary stream。 The Serializable attribute
is used by the BinaryFormatter as an indicator that the Ticket type is allowed to be serialized and
deserialized。 In essence; converting from a text stream to a binary stream means to marshal a
text…defined ticket into a …defined ticket; and that is then marshaled into a binary…defined
ticket。
Converting a Binary Stream into a Text Stream
Converting a