views:

285

answers:

1

Currently I have written a function to deserialize XML as seen below. How do I change it so I don't have to replace the type every time I want to serialize another object type ? The current object type is cToolConfig. How do I make this function generic ?

       Public Shared Function DeserializeFromXML(ByRef strFileNameAndPath As String) As XMLhandler.XMLserialization.cToolConfig
        Dim deserializer As New System.Xml.Serialization.XmlSerializer(GetType(cToolConfig))
        Dim srEncodingReader As IO.StreamReader = New IO.StreamReader(strFileNameAndPath, System.Text.Encoding.UTF8)
        Dim ThisFacility As cToolConfig

        ThisFacility = DirectCast(deserializer.Deserialize(srEncodingReader), cToolConfig)
        srEncodingReader.Close()
        srEncodingReader.Dispose()

        Return ThisFacility
    End Function


    Public Shared Function DeserializeFromXML1(ByRef strFileNameAndPath As String) As System.Collections.Generic.List(Of XMLhandler.XMLserialization.cToolConfig)
        Dim deserializer As New System.Xml.Serialization.XmlSerializer(GetType(System.Collections.Generic.List(Of cToolConfig)))
        Dim srEncodingReader As IO.StreamReader = New IO.StreamReader(strFileNameAndPath, System.Text.Encoding.UTF8)
        Dim FacilityList As System.Collections.Generic.List(Of cToolConfig)
        FacilityList = DirectCast(deserializer.Deserialize(srEncodingReader), System.Collections.Generic.List(Of cToolConfig))
        srEncodingReader.Close()
        srEncodingReader.Dispose()

        Return FacilityList
    End Function
+2  A: 

Is this what you mean?

Public Shared Function DeserializeFromXML(Of T)(ByRef strFileNameAndPath As String) As T
    Dim deserializer As New System.Xml.Serialization.XmlSerializer(GetType(T))
    Dim srEncodingReader As IO.StreamReader = New IO.StreamReader(strFileNameAndPath, System.Text.Encoding.UTF8)
    Dim ThisFacility As T

    ThisFacility = DirectCast(deserializer.Deserialize(srEncodingReader), T)
    srEncodingReader.Close()
    srEncodingReader.Dispose()

    Return ThisFacility
End Function


Public Shared Function DeserializeFromXML1(Of T)(ByRef strFileNameAndPath As String) As System.Collections.Generic.List(Of T)
    Dim deserializer As New System.Xml.Serialization.XmlSerializer(GetType(System.Collections.Generic.List(Of T)))
    Dim srEncodingReader As IO.StreamReader = New IO.StreamReader(strFileNameAndPath, System.Text.Encoding.UTF8)
    Dim FacilityList As System.Collections.Generic.List(Of T)
    FacilityList = DirectCast(deserializer.Deserialize(srEncodingReader), System.Collections.Generic.List(Of T))
    srEncodingReader.Close()
    srEncodingReader.Dispose()

    Return FacilityList
End Function

Note that you can put constraints on T, such as:

Public Shared Function DeserializeFromXML(Of T As Class)

And even put multiple constraints, such as:

Public Shared Function DeserializeFromXML(Of T As {Class, New, IDisposable})
M.A. Hanin
Yes this is exactly what I mean. Very nice, thank you !
Quandary