2

请指导我如何从泛型类型参数中获取类型,在以下情况下:

  Public Function DerializeFromFile(Of T)(ByVal fileName As String) As T
    Dim serializer As New XmlSerializer(T.GetType()) '--------------- HELP !!!???
    Dim fileStream = New FileStream(fileName,
                    FileMode.Open, FileAccess.Read, FileShare.Read)
    Dim loadedObject As T = DirectCast(serializer.Deserialize(fileStream), T)
    Return loadedObject
  End Function
4

1 回答 1

1

Change it to:

Dim serializer As New XmlSerializer(GetType(T))

In VB, the GetType operator returns the Type object for the given type.

于 2012-11-27T12:31:28.530 回答