简体   繁体   中英

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

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

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})

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM