VB.NET, make a function with return type generic ?

Posted by Quandary on Stack Overflow See other posts from Stack Overflow or by Quandary
Published on 2010-04-29T09:08:05Z Indexed on 2010/04/29 9:17 UTC
Read the original article Hit count: 315

Filed under:
|
|
|

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

© Stack Overflow or respective owner

Related posts about vb.net

Related posts about c#