Anindya Chatterjee
Anindya Chatterjee

Reputation: 5964

How to xml serialize an event in C#

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?

Upvotes: 2

Views: 720

Answers (2)

Shahin
Shahin

Reputation: 12851

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

Upvotes: 1

Darin Dimitrov
Darin Dimitrov

Reputation: 1039160

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.

Upvotes: 3

Related Questions