Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Do .NET 3/4 Permits Events to be Binary De/Serialized?

In .NET (at least <=2) there's a problem serializing objects that raise events when those events are handled by a non-serializable object (like a Windows Form).

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). A side effect of this is that any object which handles events raised by the object being serialized will be considered part of the object graph and will be serialized too.

Some workarounds could be found, implementing custom serialization or using delegates instead of events:

  • http://www.codeproject.com/KB/vb/serializevbclasses.aspx
  • http://www.lhotka.net/WeBlog/CommentView.aspx?guid=776f44e8-aaec-4845-b649-e0d840e6de2c

However none of them seems to completely satisfy the authors and users.

Does the .NET 3 / 4 solve this problem?

like image 774
serhio Avatar asked Feb 25 '10 17:02

serhio


People also ask

What is Binary serialization in C#?

Serialization can be defined as the process of storing the state of an object to a storage medium. During this process, the public and private fields of the object and the name of the class, including the assembly containing the class, are converted to a stream of bytes, which is then written to a data stream.

What is the serialization in .NET how is it affecting to .NET programming?

Serialization is a process of converting an object into a stream of data so that it can be is easily transmittable over the network or can be continued in a persistent storage location. This storage location can be a physical file, database or ASP.NET Cache.

How does serialization work in C#?

Serialization is the process of converting an object into a stream of bytes to store the object or transmit it to memory, a database, or a file. Its main purpose is to save the state of an object in order to be able to recreate it when needed. The reverse process is called deserialization.

How many types of serialization are there in C#?

There are three types of serialization in . Net : Binary Serialization, SOAP Serialization and XML Serialization. Binary serialization is the process where you convert your . NET objects into byte stream.


1 Answers

Events are handled by creation of a delegate member. If you explicitly define this member yourself, you should be able to add the NonSerialized attribute to it. See this thread for a reference.

For example:

Public Delegate Sub MyEventDelegate()

<NonSerialized()>Private m_MyEventDelegate As MyEventDelegate

Public Custom Event MyEvent As MyEventDelegate
    AddHandler(ByVal value As MyEventDelegate)
        m_MyEventDelegate = DirectCast(System.Delegate.Combine(m_MyEventDelegate, value), MyEventDelegate)
    End AddHandler

    RemoveHandler(ByVal value As MyEventDelegate)
        m_MyEventDelegate = DirectCast(System.Delegate.Remove(m_MyEventDelegate, value), MyEventDelegate)
    End RemoveHandler

    RaiseEvent()
        If m_MyEventDelegate IsNot Nothing Then
            m_MyEventDelegate.Invoke()
        End If
    End RaiseEvent
End Event
like image 189
Paul Williams Avatar answered Oct 03 '22 23:10

Paul Williams