I have the next problem: I have a big object graph being binary serialized with many fields and events marked as [NonSerializable] or [field:NonSerializable].
The problems is that, I still don’t know from-where or why (complex call tree), some Action or Func (which are fields marked as [NonSerializable] and have lambdas assigned) are serialized as you can see in the created binary file.
So, what technique could I use to detect “serialization leaks”: The members of my code where those events are hooked?
I would use reflection here, working through each property/field into related types (taking lists and other generics into account), trying to build a complete list of types we expect in the model. From there, obtain all the fields from such that are either EventHandlerList or a Delegate type (taking base-classes into account, and sub-classes where possible). Now check if any of those fields are not marked with NonSerializedAttribute to ignore serialization.
Or perhaps easier: use a serializer that doesn’t EVER write delegates; any of XmlSerializer, JavascriptSerializer, DataContractSerializer, or if you want direct binary protobuf-net. Or virtually anything that isn’t BinaryFormatter or NetDataContractSerializer.