zoukankan      html  css  js  c++  java
  • 将AE对象序列化为二进制文件 (序列化与反序列化)

    http://maruigxxg.blog.163.com/blog/static/3051816420098291149394/ 当我们编写AE程序时,通常会遇到需要存储某个AE对象的情况, 比如Layer,Element,Map,Legend,NorthArrow等等这些 举个例子说明一下:在我们编辑Featurelayer时,我们可以容易的将Feature存储在Featureclass中, 同样,如果我们向Graphicscontainer中添加了Element,我们也希望可以容易的存储Element 这样每次加载时可以将Element顺利显示出来,但是不巧的是,AE中并没有提供存储Element的方法 这个时候,我们就需要将Graphicscontainer序列化成文件,这样就可以达到存储的目的了 要知道,Featureclass存储成shapefile也好,Geodatabase也罢,都是一种文件的组织形式,也都是一种特殊意义上的序列化。 好,下面,介绍序列化的方法: 首先可以序列化的对象必须实现了IPersistStream接口, 其中IPersistStream接口是Windos中的接口,派生自 IPersist,并增加了4个函数,从流(IStream)中读写组件属性信息。 下面是各个函数的意义: IsDirty() 组件内部属性是否发生了变化。为调用者是否需要保存信息提供依据 Load() 从 IStream 中读入信息,初始化组件属性 Save() 把属性信息保存到 IStream 中 GetSizeMax() 返回信息尺寸,以便调用者事先开辟空间 ''' <summary> ''' 将AE中实现了IPersistStream接口的对象序列化为二进制文件 ''' </summary> ''' <param name="pObject">对象</param> ''' <param name="pFilePath">文件名全路径(形如:“C:\file.blb”)</param> ''' <remarks></remarks>   Sub WriteObject(ByVal pFilePath As String, ByVal pObject As Object) If Not TypeOf pObject Is IPersistStream Then MessageBox.Show("该对象不支持序列化!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information) Exit Sub End If Dim pMemoryBlobStream As IMemoryBlobStream = New MemoryBlobStream Dim pObjectStream As IObjectStream = New ObjectStream pObjectStream.Stream = pMemoryBlobStream Dim pPersistStream As IPersistStream = pObject pPersistStream.Save(pObjectStream, True) Try pMemoryBlobStream.SaveToFile(pFilePath) Catch ex As Exception MessageBox.Show("序列化文件路径不合法!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information) End Try End Sub   ''' <summary> ''' 从序列化文件中读取对象(反序列化) ''' </summary> ''' <param name="pObject">对象</param> ''' <param name="pFilePath">文件名全路径(形如:“C:\file.blb”)</param> ''' <remarks></remarks>   Sub ReadObject(ByVal pFilePath As String, ByRef pObject As Object) If Not TypeOf pObject Is IPersistStream Then MessageBox.Show("该对象不支持序列化!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information) Exit Sub End If If Not System.IO.File.Exists(pFilePath) Then MessageBox.Show("序列化文件不存在!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information) Exit Sub End If Dim pMemoryBlobStream As IMemoryBlobStream = New MemoryBlobStream pMemoryBlobStream.LoadFromFile(pFilePath) Dim pObjectStream As IObjectStream = New ObjectStream pObjectStream.Stream = pMemoryBlobStream Dim pPersistStream As IPersistStream = pObject pPersistStream.Load(pObjectStream) End Sub
  • 相关阅读:
    乱七八杂
    转化跟踪设置说明
    Photoshop图象切片保存为网页HTML(DIV+CSS布局)的方法
    HTTP 错误 403.14
    打开asp出现An error occurred on the server when processing the URL
    ADODB.Connection 错误 '800a0e7a'
    修改客户端连接的服务器IP地址(内部使用)
    关于云计算的10个常见问题解答
    云安全的11个挑战及应对策略
    如何鉴别一个区块链项目的真假?
  • 原文地址:https://www.cnblogs.com/adodo1/p/4327235.html
Copyright © 2011-2022 走看看