打印本文 打印本文 关闭窗口 关闭窗口
把握VB.NET中的流(Stream) (三)
作者:武汉SEO闵涛  文章来源:敏韬网  点击数4033  更新时间:2009/4/23 19:01:05  文章录入:mintao  责任编辑:mintao
      ("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)

以存储序列化数据的文件为参数,调用一个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.FileMod

上一页  [1] [2] [3] [4]  下一页

打印本文 打印本文 关闭窗口 关闭窗口