i am trying to build an application that uses plugins to extend the business
functionality of an application. i've decided to use inheritance of
abstract classes as the mechanism to do this. moreover, i'm using a factory
pattern to control the creation of these classes (see full code below).
i'm having a bit of difficulty getting the deserialization process to work
smoothly.
when i call Save in the Abstract class, the XmlSerializer includes the
properties from both the Abstract and Derived classes. the Xml uses
<Derived> as the class' 'delimiter'. this works as expected.
when i try to deserialize, it's more of a challenge.
in the Application.Ope n method, i return an Abstract class. i would like to
be able to deserialize any of the serialized derived classes (including ones
to be developed) by using:
Dim XS As Xml.Serializati on.XmlSerialize r = New
Xml.Serializati on.XmlSerialize r(GetType(Abstr act))
unfortunately, this causes an error. if instead i try:
Dim XS As Xml.Serializati on.XmlSerialize r = New
Xml.Serializati on.XmlSerialize r(GetType(Deriv ed))
the code works. the difficulty is knowing what the type is before
attempting to deserialize. perhaps i should examine the Xml to determine
the type before calling the XmlSerializer.
I would appreciate any thoughts on the matter. Perhaps there is an
open-source application that does something similar so i can use it.
Thanks,
Craig Buchanan
<application>
Public Class Application
Public Function NewInstance(ByV al type As String) As Abstract
Dim instance As Abstract
Select Case type
Case "Derived"
instance = New Derived
End Select
instance.Id = Guid.NewGuid
Return instance
End Function
Public Function Open(ByVal source As String) As Abstract
Dim SR As New IO.StreamReader (source)
Dim XS As Xml.Serializati on.XmlSerialize r = New
Xml.Serializati on.XmlSerialize r(GetType(Abstr act))
Return XS.Deserialize( SR)
SR.Close()
End Function
End Class
</application>
<abstract>
Public MustInherit Class Abstract
Private _Id As Guid
Private _Name As String
Public Property Id() As Guid
Get
Return _Id
End Get
Set(ByVal value As Guid)
_Id = value
End Set
End Property
Public Property Name() As String
Get
Return _Name
End Get
Set(ByVal value As String)
_Name = value
End Set
End Property
Public Sub SaveAs(ByVal destination As String)
Dim SW As New IO.StreamWriter (destination)
Dim XS As Xml.Serializati on.XmlSerialize r = New
Xml.Serializati on.XmlSerialize r(Me.GetType)
XS.Serialize(SW , Me)
SW.Close()
End Sub
End Class
</abstract>
<derived>
Public Class Derived
Inherits Abstract
Private _Description As String
Public Property Description() As String
Get
Return _Description
End Get
Set(ByVal value As String)
_Description = value
End Set
End Property
End Class
</derived>
functionality of an application. i've decided to use inheritance of
abstract classes as the mechanism to do this. moreover, i'm using a factory
pattern to control the creation of these classes (see full code below).
i'm having a bit of difficulty getting the deserialization process to work
smoothly.
when i call Save in the Abstract class, the XmlSerializer includes the
properties from both the Abstract and Derived classes. the Xml uses
<Derived> as the class' 'delimiter'. this works as expected.
when i try to deserialize, it's more of a challenge.
in the Application.Ope n method, i return an Abstract class. i would like to
be able to deserialize any of the serialized derived classes (including ones
to be developed) by using:
Dim XS As Xml.Serializati on.XmlSerialize r = New
Xml.Serializati on.XmlSerialize r(GetType(Abstr act))
unfortunately, this causes an error. if instead i try:
Dim XS As Xml.Serializati on.XmlSerialize r = New
Xml.Serializati on.XmlSerialize r(GetType(Deriv ed))
the code works. the difficulty is knowing what the type is before
attempting to deserialize. perhaps i should examine the Xml to determine
the type before calling the XmlSerializer.
I would appreciate any thoughts on the matter. Perhaps there is an
open-source application that does something similar so i can use it.
Thanks,
Craig Buchanan
<application>
Public Class Application
Public Function NewInstance(ByV al type As String) As Abstract
Dim instance As Abstract
Select Case type
Case "Derived"
instance = New Derived
End Select
instance.Id = Guid.NewGuid
Return instance
End Function
Public Function Open(ByVal source As String) As Abstract
Dim SR As New IO.StreamReader (source)
Dim XS As Xml.Serializati on.XmlSerialize r = New
Xml.Serializati on.XmlSerialize r(GetType(Abstr act))
Return XS.Deserialize( SR)
SR.Close()
End Function
End Class
</application>
<abstract>
Public MustInherit Class Abstract
Private _Id As Guid
Private _Name As String
Public Property Id() As Guid
Get
Return _Id
End Get
Set(ByVal value As Guid)
_Id = value
End Set
End Property
Public Property Name() As String
Get
Return _Name
End Get
Set(ByVal value As String)
_Name = value
End Set
End Property
Public Sub SaveAs(ByVal destination As String)
Dim SW As New IO.StreamWriter (destination)
Dim XS As Xml.Serializati on.XmlSerialize r = New
Xml.Serializati on.XmlSerialize r(Me.GetType)
XS.Serialize(SW , Me)
SW.Close()
End Sub
End Class
</abstract>
<derived>
Public Class Derived
Inherits Abstract
Private _Description As String
Public Property Description() As String
Get
Return _Description
End Get
Set(ByVal value As String)
_Description = value
End Set
End Property
End Class
</derived>