SerializationException Type “is not marked as serializable” - But it is

后端 未结 4 1297
甜味超标
甜味超标 2021-01-01 19:21

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

相关标签:
4条回答
  • 2021-01-01 19:49
    [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. :-)

    0 讨论(0)
  • 2021-01-01 19:54

    Resolved

    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;
    
    0 讨论(0)
  • 2021-01-01 19:56

    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;
    
    0 讨论(0)
  • 2021-01-01 20:04

    mark BrazierMatrix2 as [Serializable]

    0 讨论(0)
提交回复
热议问题