XmlSerializer.Deserialize factory method?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Craig Buchanan

    #1

    XmlSerializer.Deserialize factory method?

    I have an Application object that i use to open saved instances of a variety
    of derived classes (a factory method). Each derived-class instance is based
    on a MustInherit base class. Here's a code snippet:

    Public Function Open(ByVal source As String) As MyBaseClass
    ...

    Dim XS As XmlSerializer = New XmlSerializer(G etType(MyBaseCl ass))

    'error on this statement if i use GetType(MyBaseC lass); no error if i
    use GetType(MyDeriv edClass)
    Return XS.Deserialize( SR)

    ...
    End Function

    The Xml is generated by a Save method in MyBaseClass:

    Public Sub SaveAs(ByVal destination As String)
    ....
    Dim XS As XmlSerializer = New XmlSerializer(M e.GetType)
    XS.Serialize(SW , Me)
    ....
    End Sub

    Obviously, i would like my factory method to correctly identify what type of
    object has beeen saved then reconstitue it. I suppose i could bury the
    class name and assembly in the xml, then use that to create the correct type
    manually, but this approach doesn't seem to be very elegant.

    can someone suggest a better approach?

    thanks a lot,

    Craig Buchanan


Working...