VB.NET集合存儲內(nèi)容解析
今天在這里,大家會詳細(xì)了解到有關(guān)VB.NET集合存儲的相關(guān)應(yīng)用方法,希望能對大家有所幫助。大多數(shù)程序處理對象集合而不是單個的對象。對于集合數(shù)據(jù),首先創(chuàng)建一個數(shù)組(或者是其他類型的集合,比如ArrayList或HashTable),用對象填充,然后一個Serialize方法就可以序列化這個集合,是不是很簡單?下面的例子,首先創(chuàng)建一個有兩個Person對象的ArrayList,然后序列化本身:
- Dim FS As New System.IO.FileStream _
- ("c:\test.txt", IO.FileMode.Create)
- Dim BinFormatter As New Binary
.BinaryFormatter()- Dim P As New Person()
- Dim Persons As New ArrayList
- P = New Person()
- P.Name = "Person 1"
- P.Age = 35
- P.Income = 32000
- Persons.Add(P)
- P = New Person()
- P.Name = "Person 2"
- P.Age = 50
- P.Income = 72000
- Persons.Add(P)
- BinFormatter.Serialize(FS, Persons)
以VB.NET集合存儲序列化數(shù)據(jù)的文件為參數(shù),調(diào)用一個BinaryFormatter實例的Deserialize方法,就會返回一個對象,然后把它轉(zhuǎn)化為合適的類型。下面的代碼反序列化文件中的所有對象,然后處理所有的Person對象:
- FS = New System.IO.FileStream _
- ("c:\test.txt", IO.FileMode.
OpenOrCreate)- Dim obj As Object
- Dim P As Person(), R As
Rectangle()- Do
- obj = BinFormatter.
Deserialize(FS)- If obj.GetType Is GetType
(Person) Then- P = CType(obj, Person)
- ' Process the P objext
- End If
- Loop While FS.Position
< FS.Length - 1- FS.Close()
下面的例子調(diào)用Deserialize方法反序列化這個集合,然后把返回值轉(zhuǎn)換為合適的類型(Person):
- FS = New System.IO.FileStream
("c:\test.txt", IO.FileMode.
OpenOrCreate)- Dim obj As Object
- Dim Persons As New ArrayList
- obj = CType(BinFormatter.
Deserialize(FS), ArrayList)- FS.Close()
VB.NET集合存儲的相關(guān)方法就為大家介紹到這里。
【編輯推薦】