How to enable publicKeyToken in class? (Serialization problem)

I hope I even ask my question correctly. I get the following exception when trying to serialize a specific object (I am familiar with using the standard attribute [Serializable])

The first random exception of type "System.Runtime.Serialization.SerializationException" occurred in mscorlib.dll

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

I can’t find where this is coming from. None of my classes inherit from Component, none of the base classes inherit from component.

I went so far as to mark EVERY delegation of \ member varialbe as [NonSerialized], and it still throws this exception every time I try to serialize.

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

+4
source share
2 answers

I’ll let you guess, because when you see this too many times, you are calculating: you have an event , and you are subscribed from this event to some user interface code or something else Component .

When using BinaryFormatter events (more precisely, a support field) are serialized. If you do not want this, make sure your events are marked:

 [field:NonSerialized] public event EventHandler SomethingHappened; 

I will also note that when using BinaryFormatter a lot of (IMO, subjective) errors occur, which I really suggest using something else. For example, XmlSerializer (mumbles something inaudible about open source binary formatting, open source is also available).

+4
source

No, PublicKey will not help you find the problem. PublicKeyToken is the key that was used to sign the assembly, and this key is used for several CLR modules.

I would start trimming the number of objects to serialize before I disappeared. Also try disabling "my code" (Tools β†’ Options β†’ Debugging) and start breaking down all exceptions - you may get a slightly better idea where it does not work in the first place.

+1
source

Source: https://habr.com/ru/post/1381193/


All Articles