开发者

SerializationException, How to avoid UserControl consuming an Event to be serialized?

I attempt to serialize an object, and it throws an exception as shown below. I assume it attempts to serialize also the UserControl that subsribes to an event of the class I try to serialize. This is not desired. So how do I avoid serializing events? (as easy as possible)

SerializationException occured:

Type 'System.Windows.Forms.UserControl' in Assembly 'System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' is not marked as serializable.

Public Sub SendData(ByVal obj As Object)

    If Client.Connected Then
        Try
            Dim ns As NetworkStream = Client.GetStream()

            Dim bf = New BinaryFormatter()
            Dim ms = New MemoryStream()

            bf.Serialize(ms, obj)   ' < Exception h开发者_开发技巧ere


how do I avoid serializing events?

The solution is to mark the event handlers in your class with [NonSerialized] attribute.


It appears that your object you are passing into your routine is not serializable. Are you passing your usercontrol to this method? If so you will need to pass what you want to serialize which I assume might be the data structure within? Otherwise try using the [Searilzable] attribute on your object and see if that works.


One option is to implement ISerializable and only serialize the stuff you want to.

Another option available to C# (I'm not sure about VB) is to make a custom add/remove code.

[NonSerialized]  // prevents the m_changed field from being serialized.
private EventHandler m_changed;

public event Changed
{
  add
  {
    m_changed += value;
  }
  remove
  {
    m_changed -= value;
  }
}


VB.NET will not allow attribute on events. Because of the way VB.NET implements events, when you serialize an object, its events get serialized too because events are actually implemented using hidden multicast delegate fields. Here is an example of how to remove a handler during serialization in vb:

      <NonSerialized()> _
  Private PropertyChangedHandlers As New Generic.List(Of PropertyChangedEventHandler)

  ''' <summary>
  ''' stores parameter of event in a local nonserializable variable
  ''' </summary>
  ''' <remarks></remarks>
  Public Custom Event PropertyChanged As PropertyChangedEventHandler Implements INotifyPropertyChanged.PropertyChanged
     AddHandler(ByVal value As PropertyChangedEventHandler)
        If PropertyChangedHandlers Is Nothing Then
           PropertyChangedHandlers = New Generic.List(Of PropertyChangedEventHandler)()
        End If
        PropertyChangedHandlers.Add(value)
     End AddHandler

     RemoveHandler(ByVal value As PropertyChangedEventHandler)
        If value Is Nothing Or PropertyChangedHandlers Is Nothing Then
           Return
        End If

        PropertyChangedHandlers.Remove(value)
     End RemoveHandler

        RaiseEvent(ByVal sender As Object, ByVal e As PropertyChangedEventArgs)
            Dim pos As Int32
            Dim count As Int32
            Dim Element As PropertyChangedEventHandler
            If Me.PropertyChangedHandlers IsNot Nothing Then
                count = PropertyChangedHandlers.Count
                If count > 0 Then
                    For pos = (count - 1) To 0 Step -1
                        Element = PropertyChangedHandlers(pos)
                        Try
                            Element.Invoke(sender, e)
                        Catch ex As Exception
                            'Ignore any error generated
                        End Try
                    Next
                End If
            End If
        End RaiseEvent
  End Event
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜