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 cToolCo开发者_JS百科nfig)
        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})
 
         
                                         
                                         
                                         
                                        ![Interactive visualization of a graph in python [closed]](https://www.devze.com/res/2023/04-10/09/92d32fe8c0d22fb96bd6f6e8b7d1f457.gif) 
                                         
                                         
                                         
                                         加载中,请稍侯......
 加载中,请稍侯......
      
精彩评论