从字典中提取属性值

时间:2015-04-05 17:21:14

标签: vb.net

我正在尝试编写一个子程序,它将从.ser文件反序列化一个字典(这个位工作正常),然后从这个字典中重新填充几个列表(这是我不能做的)。

字典包含我写的一个名为“Photo Job”的自定义类的对象(我认为),它具有ETA,notes,medium等属性。(声明为)

Dim photoJobs As New Dictionary(Of String, PhotoJob)

简而言之,我希望能够将每个特定属性的每个条目提取到一个单独的数组中(每个属性一个),我可以从那里开始。

任何帮助都会受到赞赏,我可能会以完全错误的方式解决这个问题,我是VB的新手。相关代码如下:

摄影工作班:

<Serializable()> _Public Class PhotoJob

Private intStage As Integer                             'Declare all local private variables
Private ID As String
Private timeLeft As Integer
Private material As String '
Private note As String
Private path As String
Private finished As Boolean = False
'Declare and define properties and methods of the class
Public Property productionStage() As Integer
    Get
        Return intStage
    End Get

    Set(ByVal Value As Integer)
        intStage = Value
    End Set
End Property

Public Property photoID() As String
    Get
        Return ID
    End Get

    Set(ByVal Value As String)
        ID = Value
    End Set
End Property

Public Property ETA() As Integer
    Get
        Return timeLeft
    End Get

    Set(ByVal Value As Integer)
        timeLeft = Value
    End Set
End Property

Public Property medium() As String
    Get
        Return material
    End Get

    Set(ByVal Value As String)
        material = Value
    End Set
End Property

Public Property notes() As String
    Get
        Return note
    End Get

    Set(ByVal Value As String)
        note = Value
    End Set
End Property

Public Property imagePath() As String
    Get
        Return path
    End Get

    Set(ByVal Value As String)
        path = Value
    End Set
End Property

Public Property complete() As Boolean
    Get
        Return finished
    End Get
    Set(value As Boolean)
        finished = value
    End Set
End Property

Public Sub nextStage()
    If intStage < 4 Then
        intStage += 1
    ElseIf intStage = 4 Then
        intStage += 1
        finished = True
    End If
End Sub
End Class

de / serialization中涉及的子程序:

 Private Sub BackupAllToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles BackupAllToolStripMenuItem.Click
    Dim formatter As New BinaryFormatter
    Dim backupFile As New FileStream(Strings.Replace(Strings.Replace(Now, ":", "_"), "/", ".") & ".ser", FileMode.Create, FileAccess.Write, FileShare.None)
    formatter.Serialize(backupFile, photoJobs)
    backupFile.Close()
    MsgBox("Collection saved to file")
End Sub

Private Sub RestoreFromFileToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles RestoreFromFileToolStripMenuItem.Click
    With OpenFileDialog                                                                         'Executes the following sets/gets/methods of the OpenFileDialog
        .FileName = ""
        .Title = "Open Image File"
        .InitialDirectory = "c:\"
        .Filter = "Serial Files(*.ser)|*ser"
        .ShowDialog()
    End With
    Dim backupPathStr As String = OpenFileDialog.FileName
    Dim deSerializer As New BinaryFormatter
    Dim backupFile As New FileStream(backupPathStr, FileMode.Open)
    photoJobs = deSerializer.Deserialize(backupFile)
    backupFile.Close()

End Sub

从我可以看到的使用自动菜单,保存/恢复字典工作正常。

1 个答案:

答案 0 :(得分:1)

首先,如果您使用的是VS2010 +,则可以使用自动实现的属性大大减少样板代码:

<Serializable()>
Public Class PhotoJob   
    Public Property productionStage() As Integer
    Public Property photoID() As String
    Public Property ETA() As Integer
    etc
End Class

这就是所需要的,所有样板代码都是为您处理的。第二,用这一行:

photoJobs = deSerializer.Deserialize(backupFile)

您的反序列化照片工具将是通用Object,而不是Dictionary。您应该打开Option Strict,以便VS执行这些类型的错误。这是如何反序列化为Type:

 Using fs As New FileStream(myFileName, FileMode.Open)
    Dim bf As New BinaryFormatter
    PhotoJobs= CType(bf.Deserialize(fs), Dictionary(Of String, PhotoJob))
End Using

Using关闭并处理流,CType将BF返回的对象转换为实际字典

要使用Dictionary(这与序列化无关),您需要迭代集合以获取数据:

For Each kvp As KeyValuePair(Of String, PhotoJob) In PhotoJobs
    listbox1.items.Add(kvp.value.productionStage)
    listbox2.items.Add(kvp.value.ETA)
    etc
Next

该集合由您的声明中的(String, PhotoJob)对组成,当您将它们添加到集合中时。他们以同样的方式回归。 kvp.Key将是用于在词典中标识此作业的字符串键,kvp.Value将是对PhotoJobs对象的引用。

只要VS / VB知道它是Dictionary(of String, PhotoJob)kvp.Value就会像PhotoJob的实例一样(它是)。