我希望将 XMLSerializer 放在我的“外观”类后面,这样用户就不必向构造函数提供类型信息。但是这样做是有问题的。考虑这个类:
Class XmlFormatter
Private Shared xs As XmlSerializer
Public Function Deserialize(ByVal serializationStream As Stream) As Object
Dim o As Object = Nothing
If Not xs Is Nothing Then
o = xs.Deserialize(serializationStream)
End If
Return o
End Function
Public Overloads Sub Serialize(ByVal serializationStream As Stream, ByVal graph As Object)
If xs Is Nothing Then
xs = New XmlSerializer(graph.GetType())
End If
xs.Serialize(serializationStream, graph)
End Sub
End Class
问题是这个类的用户不能在没有首先使用 Serialize 的情况下使用 Deserialize,因为 XMLSerializer 实例是在 Serialize 中创建的并且是共享的。但是在没有这个实例的情况下使用 Deserialize 只会返回 Nothing。这个问题能解决吗?