VB NonSerialized on eventhandler ignored?

VB NonSerialized on eventhandler ignored?

Old forum URL: forums.lhotka.net/forums/t/9628.aspx


Charleh posted on Wednesday, October 13, 2010

Hi all,

Simple one...

Here's my code which is almost a carbon copy of Rockys code posted a while back

<NonSerialized()> _
    Private mNonSerializable As EventHandler

    Private mSerializable As EventHandler

    Public Custom Event RecalculateNeeded As EventHandler
        AddHandler(ByVal value As EventHandler)
            If value.Target.GetType.IsSerializable Then
                mSerializable = CType([Delegate].Combine(mSerializable, value), EventHandler)
            Else
                mNonSerializable = CType([Delegate].Combine(mNonSerializable, value), EventHandler)
            End If
        End AddHandler

        RemoveHandler(ByVal value As EventHandler)
            If value.Target.GetType.IsSerializable Then
                mSerializable = CType([Delegate].Remove(mSerializable, value), EventHandler)
            Else
                mNonSerializable = CType([Delegate].Remove(mNonSerializable, value), EventHandler)
            End If
        End RemoveHandler

        RaiseEvent(ByVal sender As Object, ByVal e As EventArgs)
            If mNonSerializable IsNot Nothing Then mNonSerializable(sender, e)
            If mSerializable IsNot Nothing Then mSerializable(sender, e)
        End RaiseEvent
    End Event

And then the wire-up

  AddHandler BookAccomm.RecalculateNeeded, AddressOf RecalculateNeeded

However, when trying to bind the object I still get an error that the object which is handling the event is not marked as serializable. I've checked and the addhandler definitely combines the delegate with the nonserialized eventhandler

I'm pulling my hair out here! Any ideas?

Charleh replied on Wednesday, October 13, 2010

Doing digging with FomatterServices.GetSerializableMembers() does not return the field marked as NonSerialized.


So somewhere there is a ref to the form which I've used to handle the event which the serializer is trying to package, yet if I remove the event wire-up code, the form binds fine


Argh!

Charleh replied on Wednesday, October 13, 2010

Aha, added NotUndoable attribute to the eventhandlers and everything is OK

Does this mean that somewhere in the framework CSLA is trying to serialize undoable members regardless if NonSerialized is set?

RockfordLhotka replied on Wednesday, October 13, 2010

The undo feature is separate from serialization, but it is similar. And so it is controlled by its own attribute, and yes, you typically need to use both attributes to block serialization and undo.

Copyright (c) Marimer LLC