提问人:gegy 提问时间:9/6/2023 更新时间:9/6/2023 访问量:33
从 XML 反序列化时忽略构造函数
Ignore constructor on deserialize from XML
问:
我有一个带有List(of SomeKindOfObjects)的类。在构造函数中,我对列表应用了几个实例,以预设列表以供首次使用。用户可以在 UI 中修改列表。例如,用户清除 then 列表中的所有元素,使其为空。之后,带有列表的类被序列化为 XML。 稍后,当我将 XML 反序列化为类时,将调用构造函数,并用“默认”值再次填充列表,但它应该是空的。
以下是我的课程:
Imports System.ComponentModel
Public Class ArticleAndBOMExportEntrySettings
Implements INotifyPropertyChanged
Public Event PropertyChanged As PropertyChangedEventHandler Implements INotifyPropertyChanged.PropertyChanged
Protected Sub OnPropertyChanged(ByVal e As PropertyChangedEventArgs)
RaiseEvent PropertyChanged(Me, e)
End Sub
Sub New()
Me.ExtensionsArticle = New List(Of DocumentType)({New DocumentType() With {
.Documenttype = DocumentType.EDocumentType.a,
.Extension = $".{DocumentType.EDocumentType.a}",
.Name = DocumentType.EDocumentType.a.ToString.ToUpper},
New DocumentType() With {
.Documenttype = DocumentType.EDocumentType.a,
.Extension = $".{DocumentType.EDocumentType.a}",
.Name = DocumentType.EDocumentType.a.ToString.ToUpper}})
Me.DbUserArticle = "Test"
End Sub
'This is set on deserialze
Public Property DbUserArticle As String = ""
'This has always the value set from the constructor, also after deserialize from XML Why?
Public Property ExtensionsArticle As List(Of DocumentType)
End Class
<Serializable>
Public Class DocumentType
Implements IEquatable(Of DocumentType)
Enum EDocumentType
a
b
c
End Enum
<Browsable(False)>
Property Documenttype As EDocumentType = EDocumentType.a
Public Property Name As String = ""
<Browsable(False)>
Property Extension As String = $".{Documenttype}"
Public Shadows Function Equals(other As DocumentType) As Boolean Implements IEquatable(Of DocumentType).Equals
Return Me.Name = other.Name
End Function
End Class
当xml从XML反序列化时,如何忽略构造函数? 奇怪的是:其他属性是从XML中设置的,而不是从列表中设置的。如果我不从构造函数中初始化列表属性,则一切正常。
答: 暂无答案
评论