我正在序列化一个对象,当加载回时,如果缺少一个字段,我想设置一个特定的值。例如,如果在版本2上添加了一个字段,则在加载由版本1生成的文件时,我想设置一个值。
<ProtoContract()>
Public Class Settings
' ... other members
<ProtoMember(33)>
Public AutoZoom As Boolean
'Load from a file
Friend Shared Function Load(filePath as string) As Settings
Dim result As Settings
Try
If IO.File.Exists(filePath) Then
Using s As New IO.FileStream(filePath, IO.FileMode.Open)
result = Serializer.Deserialize(Of Settings)(s)
End Using
Else
result = CreateNew()
End If
Catch ex As Exception
result = CreateNew()
End Try
Return result
End Function
Public Shared Function CreateNew() As Settings
Dim n = New Settings()
Return n
End Function
Private Sub New()
AutoZoom = TRUE
end sub
End Class
我试图使用构造函数,认为它会在字段反序列化之前运行。但是,当从序列化文件加载对象时,某些字段将使用文件内的值加载,但其他一些字段将保留构造函数设置的值,并且文件内的值将被忽略。
人事军官