2

I have some events in a class. While I serialize them using NetDataContractSerializer, the events are not getting serialized. Is there any way I can xml serialize an event like BinarySerializer does?

Anindya Chatterjee
  • 5,824
  • 13
  • 58
  • 82

2 Answers2

3

No, there is no way. The XML format do not preserve type information such as the BinarySerializer. Contrary to the binary format used by the binary serializer, XML is an interoperable format and because events are .NET specific artifacts, they cannot be transported.

Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
1

Do not serialize delegates and events blindly

usually, you should not serialize your class's delegates or events. This is because serialization takes he full object graph into play, and delegates by nature will serialize your subscriber list into the mix (invocation list). you can never make sure all your subscribers are serializable, can you?

so, you should put [NonSerialized] on delegates.

on events (with the event keyword) you can use the

[Field:NonSerialized] attribute ("Field" is in System.Reflection)

Source

Shahin
  • 12,543
  • 39
  • 127
  • 205