4
votes

Hopefully I'm even asking my question correctly. I'm getting the following exception while trying to serialize a specific object (I'm familar with the using the standard [Serializable] attribute)

A first chance exception of type 'System.Runtime.Serialization.SerializationException' occurred in mscorlib.dll

Additional information: Type 'System.ComponentModel.Component' in Assembly 'System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' is not marked as serializable.

I cannot find where the heck this is coming from. None of my classes inherit from Component, none of thier base classes inherit from component.

I've gotten to the point where I'm marking EVERY delegate\member varialbe as [NonSerialized], and its still throwing this exception every time I attempt to serialize.

So my question is: can I use this PublicKeyToken, and find what exact class\member is attempting to serialize?

2
Perhaps none of your types inherit from Component, but does one of them have a field with a type which inherits from Component? There must be something referencing Component, otherwise this would point to a severe bug in your serializer! ;-) - Adam Ralph

2 Answers

4
votes

I'll give a guess based on having seen this too many times to count: you have an event, and you have subscribed from that event to some UI code or something else Component related.

When using BinaryFormatter, events (or rather, the backing field) are serialized. If you don't want this, ensure your events are marked:

[field:NonSerialized]
public event EventHandler SomethingHappened;

I'll also note that so many (IMO, subjective) bad things happen when using BinaryFormatter that I really would suggest using something else. XmlSerializer for example (mutters something inaudible about a quite-populate open-source binary formatter also being available).

1
votes

No, PublicKey will not help you in finding the problem. PublicKeyToken is key that was used to sign the assembly and this key is used for more than one CLR assembly.

I would start trimming amount of objects to serialize till I the issue disappear. Also try to turn off "my code" (Tools -> Options -> Debugging) and start breaking on all exceptions - you may get slightly better idea where it fails first.