对于VB.NET集合存储内容相关详细讲述
今天在这里,大家会详细了解到有关VB.NET集合存储的相关应用方法,希望能对大家有所帮助。大多数程序处理对象集合而不是单个的对象。对于集合数据,首先创建一个数组(或者是其他类型的集合,比如ArrayList或HashTable),健康知识咨询重庆无精症治疗费用用对象填充,然后一个Serialize方法就可以序列化这个集合,是不是很简单?下面的例子,首先创建一个有两个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集合存储序列化数据的文件为参数,调用一个BinaryFormatter实例的Deserialize方法,就会返回一个对象,然后把它转化为合适的类型。下面的代码反序列化文件中的所有对象,然后处理所有的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()
下面的例子调用Deserialize方法反序列化这个集合,然后把返回值转换为合适的类型(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集合存储的相关方法就为大家介绍到这里。
浙公网安备 33010602011771号