|
dev
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
An object that serializes and deserializes itself dynamically?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 You will have to copy the members from the deserialized instance returned.
-- Show quoteHTH, Kevin Spencer Microsoft MVP Chicken Salad Surgery What You Seek Is What You Get. "Steve Bugden" <SteveBug***@discussions.microsoft.com> wrote in message news:E1C40579-54A0-4C8C-A168-CE1D7C4CD43F@microsoft.com... > 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 OK Thanks.
Show quote "Kevin Spencer" wrote: > You will have to copy the members from the deserialized instance returned. > > -- > HTH, > > Kevin Spencer > Microsoft MVP > Chicken Salad Surgery > > What You Seek Is What You Get. > > "Steve Bugden" <SteveBug***@discussions.microsoft.com> wrote in message > news:E1C40579-54A0-4C8C-A168-CE1D7C4CD43F@microsoft.com... > > 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 > > > Hi,
For anyone who may be interested I have worked out a way for an object to deserialise itself. Please see code listing below. Following on from the previous example this function would sit in the Person class.(in c#, sorry as previous code was in vb.net). I would be pleased to know if anyone can see any problem with this code. Regards, public virtual void Deserialise() { XmlSerializer serializer = new XmlSerializer(typeof(Person)); Person objSettings; //Deserialise the file to create the main web site object serializer = new XmlSerializer(typeof(Person)); FileStream fstFileStream = new FileStream(m_strFileName, FileMode.Open); objSettings = (Person)serializer.Deserialize(fstFileStream); if (fstFileStream != null) { fstFileStream.Close(); fstFileStream = null; } //Assign the properties Type objectType = objSettings.GetType(); PropertyInfo[] properties = objectType.GetProperties(); Type objectTypeThis = this.GetType(); PropertyInfo[] propertiesThis = objectTypeThis.GetProperties(); foreach (PropertyInfo property in properties) { foreach (PropertyInfo propertyThis in propertiesThis) { if (propertyThis.Name == property.Name) { propertyThis.SetValue(this, property.GetValue(objSettings, null), null); } } } } |
|||||||||||||||||||||||