请指导我如何从泛型类型参数中获取类型,情况如下:
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
答案 0 :(得分:1)
将其更改为:
Dim serializer As New XmlSerializer(GetType(T))
在VB中,GetType
运算符返回给定类型的Type
对象。