【问题标题】:Trying to save an array of structures to file and load from file (serialize, deserialize)尝试将结构数组保存到文件并从文件加载(序列化、反序列化)
【发布时间】:2017-03-08 02:06:27
【问题描述】:

这是我的数组:

    Public RacersArray(AmountOfRacers - 1) As Racer

<Serializable()> Public Structure Racer
    Public Name As String
    Public CleatSize As String
    Public SkillLevel As String
    Public Height As String
    Public Team As String
    Public CompatibilityArr() As String
End Structure

<Serializable()> Public Structure Compatibility
    Public Name As String
    Public Score As Integer
End Structure

下面是我用来尝试从文件保存和加载的代码。该文件正在填充看起来像正确的乱码,但是在加载数组时,它的索引仍然是“什么都没有”

    Public Sub RacersInputSAVE()
    Dim bf As New System.Runtime.Serialization.Formatters.Binary.BinaryFormatter
    Dim fStream As New FileStream(SaveLocation, FileMode.Create)

    bf.Serialize(fStream, InputRacers.RacersArray) ' write to file

    fStream.Close()
End Sub

Public Sub RacersInputLOAD()
    Dim bf As New System.Runtime.Serialization.Formatters.Binary.BinaryFormatter
    Dim fStream As New FileStream(LoadLocation, FileMode.Open)

    InputRacers.RacersArray = bf.Deserialize(fStream) ' read from file

    fStream.Close()
End Sub

【问题讨论】:

  • 实际上您的代码应该可以正常工作。你确定SaveLocationLoadLocation 指向同一个文件吗?也许拥有一个FileLocation 并将其作为参数传递给您的潜艇会更好。也可以考虑试试 Using 块,它更直观。

标签: arrays vb.net serialization structure record


【解决方案1】:

第一件事可能是错误的:

Public CompatibilityArr() As String

根据名称,这似乎应该是第二个Compatibility 结构。如果是这样,应该是:

Public CompatibilityArr As Compatibility()

不清楚这是否是所描述问题的一部分(but when loading the array and it's indexes are still 'nothing' 含糊不清,因为有多个数组)。否则不会使用第二个结构。

下一步,设置Option Strict On。反序列化时,BinaryFormatter 总是返回Object,需要将其转换为正确的类型:

Racers = DirectCast(bf.Deserialize(fs), Racer()) ' read from file

通过这 2 项更改,所有数据对我来说都很好。

第三, Structure 不是正确的类型。这些应该是类,而不是公共字段/成员,使用属性,尤其是在涉及任何数据绑定时:

<Serializable()> 
Public Class Racer
    Public Property Name As String
    ...

此外,任何实现Dispose()方法的东西,例如FileStream,都应该在Using块中使用,该块将关闭并处理目标对象:

Dim bf As New BinaryFormatter
Using fs As New FileStream(fileName, FileMode.Create)
    bf.Serialize(fs, Racers) ' write to file
End Using

最后,您可能会发现一些 NET 集合(例如 List(Of Racer))比数组更易于使用。了解它们的工作原理大约需要 15 分钟。

MSDN:Choosing Between Class and Struct

【讨论】:

  • 感谢您的所有精彩建议!现在一切都令人满意,我非常感谢你。以后我一定会复习我的清单 :)
  • 一个列表非常像一个数组,但你不必知道它有多大——它们会自动增长。为此:Racers = DirectCast(bf.Deserialize(fs), List(Of Racer)) 赛车手所在的位置:Dim Racers As List(Of Racer) 这是您需要了解的有关列表的 85%
  • 另见 Five Minute Intro to Classes and Lists 还添加了指向 MSDN 上关于类与结构的精彩文章的链接
【解决方案2】:

由于某种原因,我从来没有运气好使用 BinaryFormatter 序列化到文件。确切的原因已经被时间的迷雾迷失了,但我知道 SoapFormatter 对我来说是正确的:

    Using oStream As Stream = File.Open(SaveLocation, FileMode.Create, IO.FileAccess.Write)
        If oStream IsNot Nothing Then
            Call (New System.Runtime.Serialization.Formatters.Soap.SoapFormatter).Serialize(oStream, InputRacers.RacersArray)
        End If
    End Using

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-27
    • 1970-01-01
    • 2011-08-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多