VB.NET类序列化设计器错误

时间:2015-02-12 04:56:29

标签: vb.net debugging serialization properties designer

我想制作自己的菜单。因此,我想为菜单主体和菜单项使用面板。

这是我的班级(简而言之):

Public Class MenuM
    Inherits System.Windows.Forms.Panel
    '
    Private collectionValue As List(Of MenuMItem)
    '
    Public Property Collection As List(Of MenuMItem)
        Set(value As List(Of MenuMItem))
            collectionValue = value
        End Set
        Get
            Return collectionValue
        End Get
    End Property
    '
    Public Sub New()
        Me.Collection = New List(Of MenuMItem)
    End Sub
    '
    Public Class MenuMItem
        Inherits System.Windows.Forms.Panel
    End Class
End Class

当我在构造函数中添加Me.Collection = New List(Of MenuMItem)(这是问题?)行时,设计器和调试器都会收到序列化错误消息:

尝试将我的类作为控件添加到主窗体时出现此错误:

Error 1

这是我在尝试开始调试时得到的:

Error 2

我尝试在<Serializable()>Public Class MenuM之上添加Public Class MenuMItem。这没有用。我不明白这个问题。有人可以帮忙吗?

非常感谢!


这是解决方案吗?(呵呵)

Imports System
Imports System.Runtime.Serialization.Formatters.Binary
Imports System.Runtime.Serialization
Imports System.Security.Permissions
Imports System.IO


<Serializable()>
Public Class MenuM
    Inherits System.Windows.Forms.Panel
    Implements ISerializable
    '
    Public Sub New()

    End Sub
    '
    Protected Sub New(ByVal info As SerializationInfo, _
    ByVal context As StreamingContext)
        If info Is Nothing Then
            Throw New System.ArgumentNullException("info")
        End If
        collectionValue = info.GetValue("AltMenuMItemList", GetType(List(Of MenuMItem)))
    End Sub
    '
    <SecurityPermission(SecurityAction.LinkDemand, _
    Flags:=SecurityPermissionFlag.SerializationFormatter)> _
    Public Overridable Sub GetObjectData _
    (ByVal info As SerializationInfo, _
    ByVal context As StreamingContext) _
    Implements ISerializable.GetObjectData
        If info Is Nothing Then
            Throw New System.ArgumentNullException("info")
        End If
        info.AddValue("AltMenuMItemList", New List(Of MenuMItem))
    End Sub
    '
    Private collectionValue As List(Of MenuMItem)
    '
    Public Property Collection As List(Of MenuMItem)
        Set(value As List(Of MenuMItem))
            collectionValue = value
        End Set
        Get
            Return collectionValue
        End Get
    End Property
    '
    <Serializable()>
    Public Class MenuMItem
        Inherits System.Windows.Forms.Panel
        Implements ISerializable

        Private textValue As String
        Overloads Property Text As String
            Set(value As String)
                textValue = value
            End Set
            Get
                Return textValue
            End Get
        End Property

        <SecurityPermission(SecurityAction.LinkDemand, _
        Flags:=SecurityPermissionFlag.SerializationFormatter)> _
        Public Sub GetObjectData(info As System.Runtime.Serialization.SerializationInfo, _
                             context As System.Runtime.Serialization.StreamingContext) _
    Implements System.Runtime.Serialization.ISerializable.GetObjectData

            If info Is Nothing Then
                Throw New System.ArgumentNullException("info")
            End If
            textValue = info.GetValue("AltText", GetType(String))
        End Sub

        Protected Sub New(ByVal info As SerializationInfo, _
       ByVal context As StreamingContext)
            If info Is Nothing Then
                Throw New System.ArgumentNullException("info")
            End If
            info.AddValue("AltText", "XXX")
        End Sub

        Sub New()

        End Sub
    End Class
End Class

来源: https://msdn.microsoft.com/en-us/library/system.runtime.serialization.iserializable.getobjectdata(v=vs.110).aspx?cs-save-lang=1&cs-lang=vb#code-snippet-2

0 个答案:

没有答案