通用反序列化

时间:2012-11-27 11:16:42

标签: .net vb.net generics serialization xml-serialization

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

  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

1 个答案:

答案 0 :(得分:1)

将其更改为:

Dim serializer As New XmlSerializer(GetType(T))

在VB中,GetType运算符返回给定类型的Type对象。