GetType 上的 InvalidOperationException
InvalidOperationException on GetType
我尝试使用 XMLSerializer 在 VB 中序列化一个 Class。
但是当我为我的 Class 调用 GetType 时,我得到了一个 InvalidOperationException 错误。
Dim Playlist_serialize As New XmlSerializer(p.GetType)
这是我的 class :
Public Class Playlist
Private p_name As String
Private p_elements As List(Of Playlist_element)
Sub New()
p_elements = New List(Of Playlist_element)
End Sub
Public Property Name() As String
Get
Name = p_name
End Get
Set(value As String)
p_name = value
End Set
End Property
Public Property Elements() As List(Of Playlist_element)
Get
Elements = p_elements
End Get
Set(value As List(Of Playlist_element))
p_elements = value
End Set
End Property
这是我的 Playlist_element :
Public Class Playlist_element
Private p_Name As String
Private p_Type As String
Private p_Genre As String
Public Property Name() As String
Get
Name = p_Name
End Get
Set(value As String)
p_Name = value
End Set
End Property
Public Property Type() As String
Get
Type = p_Type
End Get
Set(value As String)
p_Type = value
End Set
End Property
Public Property Genre() As String
Get
Genre = p_Genre
End Get
Set(value As String)
p_Genre = value
End Set
End Property
Sub New(ByVal name As String, ByVal type As String, ByVal genre As String)
Me.Name = name
Me.Genre = genre
Me.Type = Type
End Sub
End Class
Playlist_element
的编码方式存在几个问题。首先,您的 属性 吸气剂是错误的。他们需要 return 支持字段:
Public Property Name() As String
Get
' this does nothing:
'Name = p_Name
Return p_Name
End Get
Set(value As String)
p_Name = value
End Set
End Property
接下来,即使可以,我也不会使用 Type
作为 属性 名称。如果深入内部异常并查看消息,它会告诉您它无法序列化 PlayList_element
,因为它没有简单的构造函数。所有序列化程序都需要这个,因为他们不知道如何使用:
Sub New(ByVal name As String, ByVal type As String, ByVal genre As String)
p_Name = name
p_Genre = genre
p_Type = type
End Sub
' add:
Public Sub New()
End Sub
它应该可以正常工作。我应该注意,从 VS2010 开始,您可以使用自动实现的属性并跳过 很多 该代码:
Public Class Element
Public Property Name() As String
Public Property Type() As String
Public Property Genre() As String
Sub New(name As String, type As String, genre As String)
_Name = name
_Genre = genre
_Type = type
End Sub
Public Sub New()
End Sub
End Class
VS 提供 "hidden" 支持字段,与 _Name
、_Genre
等
我尝试使用 XMLSerializer 在 VB 中序列化一个 Class。 但是当我为我的 Class 调用 GetType 时,我得到了一个 InvalidOperationException 错误。
Dim Playlist_serialize As New XmlSerializer(p.GetType)
这是我的 class :
Public Class Playlist
Private p_name As String
Private p_elements As List(Of Playlist_element)
Sub New()
p_elements = New List(Of Playlist_element)
End Sub
Public Property Name() As String
Get
Name = p_name
End Get
Set(value As String)
p_name = value
End Set
End Property
Public Property Elements() As List(Of Playlist_element)
Get
Elements = p_elements
End Get
Set(value As List(Of Playlist_element))
p_elements = value
End Set
End Property
这是我的 Playlist_element :
Public Class Playlist_element
Private p_Name As String
Private p_Type As String
Private p_Genre As String
Public Property Name() As String
Get
Name = p_Name
End Get
Set(value As String)
p_Name = value
End Set
End Property
Public Property Type() As String
Get
Type = p_Type
End Get
Set(value As String)
p_Type = value
End Set
End Property
Public Property Genre() As String
Get
Genre = p_Genre
End Get
Set(value As String)
p_Genre = value
End Set
End Property
Sub New(ByVal name As String, ByVal type As String, ByVal genre As String)
Me.Name = name
Me.Genre = genre
Me.Type = Type
End Sub
End Class
Playlist_element
的编码方式存在几个问题。首先,您的 属性 吸气剂是错误的。他们需要 return 支持字段:
Public Property Name() As String
Get
' this does nothing:
'Name = p_Name
Return p_Name
End Get
Set(value As String)
p_Name = value
End Set
End Property
接下来,即使可以,我也不会使用 Type
作为 属性 名称。如果深入内部异常并查看消息,它会告诉您它无法序列化 PlayList_element
,因为它没有简单的构造函数。所有序列化程序都需要这个,因为他们不知道如何使用:
Sub New(ByVal name As String, ByVal type As String, ByVal genre As String)
p_Name = name
p_Genre = genre
p_Type = type
End Sub
' add:
Public Sub New()
End Sub
它应该可以正常工作。我应该注意,从 VS2010 开始,您可以使用自动实现的属性并跳过 很多 该代码:
Public Class Element
Public Property Name() As String
Public Property Type() As String
Public Property Genre() As String
Sub New(name As String, type As String, genre As String)
_Name = name
_Genre = genre
_Type = type
End Sub
Public Sub New()
End Sub
End Class
VS 提供 "hidden" 支持字段,与 _Name
、_Genre
等