7,763
社区成员
发帖
与我相关
我的任务
分享
'Class1
'必须放在 ActiveX Dll 或 ActiveX Exe 工程中,并且设置类的属性
' Instancing = 5 - MultiUse
' Persistable = 1 - Persistable
Option Explicit
Private m_Name As String
Public Property Get Name() As String
Name = m_Name
End Property
Public Property Let Name(ByVal RHS As String)
m_Name = RHS
End Property
Private Sub Class_ReadProperties(PropBag As PropertyBag)
m_Name = PropBag.ReadProperty("Name", vbNullString)
End Sub
Private Sub Class_WriteProperties(PropBag As PropertyBag)
PropBag.WriteProperty "Name", m_Name, vbNullString
End Sub
'测试代码,要引用 Class1 所在工程或二进制 dll/exe
Option Explicit
Sub Main()
Serialize
Deserialize
End Sub
Sub Serialize()
Dim c As Class1
Dim PropBag As PropertyBag
Dim aContents() As Byte
Dim hFile As Integer
Set c = New Class1
c.Name = "ABC"
Set PropBag = New PropertyBag
PropBag.WriteProperty "Object", c
aContents = PropBag.Contents
hFile = FreeFile()
Open App.Path & "\Object.dat" For Binary Access Write As #hFile
Put #hFile, , aContents
Close #hFile
End Sub
Sub Deserialize()
Dim c As Class1
Dim PropBag As PropertyBag
Dim aContents() As Byte
Dim hFile As Integer
hFile = FreeFile()
Open App.Path & "\Object.dat" For Binary Access Read As #hFile
ReDim aContents(LOF(hFile) - 1)
Get #hFile, , aContents
Close #hFile
Set PropBag = New PropertyBag
PropBag.Contents = aContents
Set c = PropBag.ReadProperty("Object", Nothing)
Debug.Print c.Name
End Sub