gpt4 book ai didi

.net - 无法在 VB.NET 中的可序列化单例类的实例上调用 Serialize()

转载 作者:行者123 更新时间:2023-12-03 05:41:16 25 4
gpt4 key购买 nike

我有一个 VB.NET 单例类,它实现了 Serialized:

Imports System.IO
Imports System.Runtime.Serialization

<Serializable()> Public Class CoordinateHistory
Private Shared _thisInstance As CoordinateHistory

Private gpsHistory As Dictionary(Of DateTime, GpsTimeCoordinate)
Private gpsTimes As List(Of DateTime)

Public Event NewStatusInformation(statusInfo As String)

Protected Sub New()
gpsHistory = New Dictionary(Of DateTime, GpsTimeCoordinate)
gpsTimes = New List(Of DateTime)
End Sub

Public Shared Function getInstance() As CoordinateHistory
If _thisInstance Is Nothing Then
_thisInstance = New CoordinateHistory
End If

Return _thisInstance
End Function

Public Function getHistoryCount() As Integer
Return gpsHistory.Count
End Function

' bunch of other class functions below...
End Class

我的问题是,我实际上无法在此类的实例上调用.Serialize(),就像所有在线示例一样。我究竟做错了什么?谢谢!

最佳答案

我认为问题出在您的 protected 子系统上。如果将其更改为 Public,则可能能够正确序列化。

更新

我在使用 BinaryFormatter 序列化默认实例时没有遇到任何问题:

Dim abData As Byte()

Using oStream As New MemoryStream
Call (New BinaryFormatter).Serialize(oStream, CoordinateHistory.getInstance())

abData = oStream.ToArray()
End Using

或 XMLFormatter:

Dim sData As String

Using oStream As New MemoryStream
Dim oSerializer As New XmlSerializer(CoordinateHistory.getInstance().GetType)

oSerializer.Serialize(oStream, CoordinateHistory.getInstance())

sData = Encoding.Default.GetString(oStream.ToArray())
End Using

也许这是您正在使用的序列化框架?

关于.net - 无法在 VB.NET 中的可序列化单例类的实例上调用 Serialize(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8291442/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com