G
Guest
Hi,
I created a class that can serialize and deserialize itself dynamically.
Please see the example class below.
I can call the class to serialise and deserialise as follows:
Dim objPerson As New Person
objPerson.FamilyName = "Smith"
objPerson.FirstName = "John"
objPerson.Serialise()
objPerson = objPerson.DeSerialise
MsgBox(objPerson.FirstName & " " & objPerson.FamilyName)
This works but I would prefer it if the DeSerialise function didn't have to
return an object of the same type but simply set the values inside of
objPerson. It would be a bit confusing if the return value was not assigned
to anything.
Obviously, one solution would be to hard code the values but then I would
lose the dynamic nature of the function.
Does anyone have a better solution?
Regards,
Steve.
**************************
Imports System.IO
Imports System.Text
Imports System.Xml.Serialization
Imports System
Public Class Person
Inherits Object
Private m_strFirstName As String
Private m_strFamilyName As String
Private m_strFilename As String = "MyFile.txt"
Public Overridable Property FirstName() As String
Get
Return m_strFirstName
End Get
Set(ByVal value As String)
m_strFirstName = value
End Set
End Property
Public Overridable Property FamilyName() As String
Get
Return m_strFamilyName
End Get
Set(ByVal value As String)
m_strFamilyName = value
End Set
End Property
Public Overridable Sub Serialise()
Dim serializer As New XmlSerializer(GetType(Person))
Dim objWriter As New StreamWriter(m_strFileName)
serializer.Serialize(objWriter, Me)
objWriter.Close()
objWriter = Nothing
serializer = Nothing
End Sub
Public Overridable Function DeSerialise() As Person
Dim serializer As New XmlSerializer(GetType(Person))
Dim fstFileStream As FileStream
Dim objMMSSettings As Person
'Deserialise the file to create the main web site object
serializer = New XmlSerializer(GetType(Person))
fstFileStream = New FileStream(m_strFilename, FileMode.Open)
objMMSSettings = CType(serializer.Deserialize(fstFileStream), Person)
If Not fstFileStream Is Nothing Then
fstFileStream.Close()
fstFileStream = Nothing
End If
Return objMMSSettings
End Function
End Class
I created a class that can serialize and deserialize itself dynamically.
Please see the example class below.
I can call the class to serialise and deserialise as follows:
Dim objPerson As New Person
objPerson.FamilyName = "Smith"
objPerson.FirstName = "John"
objPerson.Serialise()
objPerson = objPerson.DeSerialise
MsgBox(objPerson.FirstName & " " & objPerson.FamilyName)
This works but I would prefer it if the DeSerialise function didn't have to
return an object of the same type but simply set the values inside of
objPerson. It would be a bit confusing if the return value was not assigned
to anything.
Obviously, one solution would be to hard code the values but then I would
lose the dynamic nature of the function.
Does anyone have a better solution?
Regards,
Steve.
**************************
Imports System.IO
Imports System.Text
Imports System.Xml.Serialization
Imports System
Public Class Person
Inherits Object
Private m_strFirstName As String
Private m_strFamilyName As String
Private m_strFilename As String = "MyFile.txt"
Public Overridable Property FirstName() As String
Get
Return m_strFirstName
End Get
Set(ByVal value As String)
m_strFirstName = value
End Set
End Property
Public Overridable Property FamilyName() As String
Get
Return m_strFamilyName
End Get
Set(ByVal value As String)
m_strFamilyName = value
End Set
End Property
Public Overridable Sub Serialise()
Dim serializer As New XmlSerializer(GetType(Person))
Dim objWriter As New StreamWriter(m_strFileName)
serializer.Serialize(objWriter, Me)
objWriter.Close()
objWriter = Nothing
serializer = Nothing
End Sub
Public Overridable Function DeSerialise() As Person
Dim serializer As New XmlSerializer(GetType(Person))
Dim fstFileStream As FileStream
Dim objMMSSettings As Person
'Deserialise the file to create the main web site object
serializer = New XmlSerializer(GetType(Person))
fstFileStream = New FileStream(m_strFilename, FileMode.Open)
objMMSSettings = CType(serializer.Deserialize(fstFileStream), Person)
If Not fstFileStream Is Nothing Then
fstFileStream.Close()
fstFileStream = Nothing
End If
Return objMMSSettings
End Function
End Class