VB6->DOT NET :: 这个旧方法的 dot Net 代码是什么?

问题描述 投票:0回答:1

我正在构建一个应用程序,其数据位于自定义数据结构数组上,其中包含多个变量和其他自定义结构的子数组...

我需要能够将该数据保存和加载到文件中,并且序列化确实起作用,直到我需要在多个应用程序上加载和保存同一文件。

我必须使用旧的 VB6 技术来达到我的目标。有没有办法在更新的代码中编写这个?

在下面的示例中,Persons 是一个结构体数组,其中包含有关人的各种信息,包括其孩子、宠物等不同大小的其他结构体的子数组。

保存代码:

        '// Get file number
        Dim fn As Integer = FreeFile()
        '// Opens the file for binary write
        FileOpen(fn, FileName, OpenMode.Binary, OpenAccess.Write)
        '// Get persons count
        Dim pc As Integer = Persons.GetUpperBound(0)
        '// Write persons count
        FilePut(fn, pc)
        '// Write Persons
        FilePut(fn, Persons)
        '// Close the file
        FileClose(fn)

加载代码:

        '// Get file number
        Dim fn As Integer = FreeFile()
        '// Opens the file for binary read
        FileOpen(fn, FileName, OpenMode.Binary, OpenAccess.Read)
        '// Get persons count
        Dim pc As Integer
        '// Read persons count
        FileGet(fn, pc)
        '// Prepare Persons array for it
        Persons = Nothing
        ReDim Persons(0 To pc)
        '// Read Persons
        FileGet(fn, Persons)
        '// Close the file
        FileClose(fn)

编辑附加信息:

当使用序列化时,这是我正在使用的。 我已经简化了一些事情,使其更容易重现,而不会暴露过多的信息。这是我编写的 2 个用于测试的应用程序的示例。

我的结构和变量如下。每个父母可能有 0 个或更多孩子:

'// Parent structure
<Serializable> Private Structure Parents
    Dim Name As String
    Dim Age As Integer
    Dim Kids() As Kids
End Structure
'// Kid structure
<Serializable> Private Structure Kids
    Dim Name As String
    Dim Age As Integer
    Dim GoesInScool As Boolean
End Structure
Private Persons() As Parents

为了保存,我使用此代码:

'// Gets BinaryFormatter
Dim BF As New System.Runtime.Serialization.Formatters.Binary.BinaryFormatter()
'// Makes new Memorystream
Dim MS As New System.IO.MemoryStream()
'// Serialize to Memorystream
BF.Serialize(MS, Persons)
'// Writes the content to file
My.Computer.FileSystem.WriteAllBytes(FileName, MS.GetBuffer(), False)

要加载,我使用此代码:

'// Loads file to a byte()
Dim bytes As Byte() = My.Computer.FileSystem.ReadAllBytes(FileName)
'// Gets BinaryFormatter
Dim BF As New System.Runtime.Serialization.Formatters.Binary.BinaryFormatter()
'// Reads data
Persons = DirectCast(BF.Deserialize(New System.IO.MemoryStream(bytes)), Parents())

我制作了 2 个应用程序:StructureSaver_App_A 和 StructureSaver_App_B。 它们是从头开始制作的,但我在所有函数上复制了完全相同的代码。

当我在 StructureSaver_App_A 上保存并尝试在 StructureSaver_App_B 上加载时,我收到以下错误代码: System.Runtime.Serialization.SerializationException:“无法找到程序集“StructureSaver_App_A,版本=1.0.0.0,Culture=neutral,PublicKeyToken=null”。”

当我在 StructureSaver_App_B 上保存并尝试在 StructureSaver_App_A 上加载时,我收到以下错误代码: System.Runtime.Serialization.SerializationException:“无法找到程序集“StructureSaver_App_B,版本=1.0.0.0,Culture=neutral,PublicKeyToken=null”。”

在 StructureSaver_App_A 上加载保存在 StructureSaver_App_A 上的文件效果非常好。 在 StructureSaver_App_B 上加载保存在 StructureSaver_App_B 上的文件效果非常好。

谢谢

.net data-structures serialization vb6 vb6-migration
1个回答
0
投票

您尝试从一个应用程序序列化对象并从另一个应用程序反序列化它。即使这些对象由于具有相同的属性等而看起来相同,但实际上它们并不是相同的。因为它们位于不同的程序集或命名空间中。

正如您所说,您正在序列化 ApplicationOne.MyObject 类型的对象并将其反序列化为 ApplicationTwo.MyObject。二进制格式化程序将失败,因为在该二进制数据中存在原始类型的名称,并且无法找到该名称,因为您处于不存在该类型的不同上下文中。您需要将这些共享类提取到额外的库中,并通过引用在应用程序之间共享它。或者转向不关心命名空间的(值)序列化。例如。 JSON.

© www.soinside.com 2019 - 2024. All rights reserved.