In Windows Forms, .NET Framework 4.0, I am trying to Serialize an instance of a class I wrote.
The class is marked as Serializable, but the form that uses the class
[field:NonSerializedAttribute()]
public event EventHandler BrazierCuttoffChanged;
Had the same problem, the PropertyChanged EventHandler of my serializable NotifyObject base class was subscribed by some viewmodels which went into the serialization queue therefore. Adding the NonSerializedAddtribute to this EventHandler saved my day. :-)
I needed to set a NonSerializedAttribute for the EventHandler.
The event delegate could not be serialized in the class (see Delegates and Serialization).
Marking the field as NonSerializedAttribute was as easy as it sounds.
From my code, I simply added this line:
[field:NonSerializedAttribute()]
public event EventHandler BrazierCuttoffChanged;
This is something you can run into with any JSON serialization, including Web API in MVC 4.
I found this post to be very helpful when I was getting a serialization except on a const int value. Any const value needs to be marked with the same attribute as in Nick Freeman's answer:
[field: NonSerializedAttribute]
const int iCantBeSerialized = 1;
mark BrazierMatrix2
as [Serializable]