0

I need to know when a child object is currently deserialized using the Microsoft XmlSerializer.

I do not want to use a global indicator which would not works in multithread.

My class is already automatically deserialized using public properties. I prefer to not manage serialization myself (I prefer to stay with automatic deserialization of public properties if possible).

I thought about 2 things:

  • Using TLS (ie Thread local storage - because XmlSerializer is single threaded)
  • Using the CallStack

But both approach are far from being elegant. I wonder if there is not a better solution like implementing a Deserializable interface or something else? I'm missing knowledge to know how to do it in a clean way.

Eric Ouellet
  • 10,996
  • 11
  • 84
  • 119
  • You can implment `IXMLSerializable`, [How To](https://stackoverflow.com/a/280077/7339946) – JSteward Mar 08 '18 at 15:59
  • And a [tutorial](https://www.codeproject.com/Articles/43237/How-to-Implement-IXmlSerializable-Correctly) – JSteward Mar 08 '18 at 16:01
  • Maybe if you explain why you need this - someone can provide a better solution. – Evk Mar 08 '18 at 16:01
  • @JSteward, using IXmlSerializable does will enable me to let the xmlSerializer deseriaize public property normally and also let me hook myself? I will give a try! – Eric Ouellet Mar 08 '18 at 16:01
  • @Evk, while deserializarion occur, I do some validations where I got exception because some fields are not yet deserialized. But it is hard to explain and long questions tend to not get answers (or lot less) – Eric Ouellet Mar 08 '18 at 16:03
  • Well the short answer is yes you can do that, but it's not that easy. It would likely be easier to refactor the class in such a way that it can be serialized/deserialized with only attribute customizations. Other options include type intializers, constructor initialization, wrapping XMLSerializer in a custom variant. Can you post a representative example of the kind of class that has the same issues your facing in the full implementation. – JSteward Mar 08 '18 at 16:07
  • I'm asking because I think there is no elegant way to do that (given your requirements), so changing design might be better. Your validation fails if fields are not deserialized, but doesn't that mean your validation depends on the order in which fields are set? What if user (not serializer) sets them in wrong order? – Evk Mar 08 '18 at 16:10
  • @evk, in fact the nicer way would have to implement an IXmlSerializer with an AfterNormalPublicFieldDeserialized() method in it :-) ! But perhaps you are right and I will do implement in my hierarchy in the base class of all classes something like AfterDeserialization() which would be more elegant than any twisted code like JSteward talk to me about! – Eric Ouellet Mar 08 '18 at 16:19
  • @JSteward thanks so much for your help. It appears a lot more complex that I previously thought it could be. Perhaps I will follow Evk recommandation and modify my hierarchy in order to create an event or a derived method "OnDeserializationComplete"... And will enable field validation after that. About a full sample, I can do one but I will have to recreate one from zero. I will see if there is no easier alternative because it will takes relatively lots of efforts - I will probably just add OnDeserializationComplete kind of event in my class hierarchy. – Eric Ouellet Mar 08 '18 at 16:23
  • @JSteward, About IXmlSerializable, do you know if there is a way to call default implementation for: ReadXml and WriteXml. I think for GetShema(), to return null will do the default behavior. I'm searching that info in your links and more but can't find it. I don't think so because the only argument is the XmlReader and there is no way to let know the XmlSerializer of what we have done with it. – Eric Ouellet Mar 08 '18 at 16:39
  • Have a look [here](https://stackoverflow.com/questions/898187/how-to-use-default-xml-serialization-from-within-custom-xml-serialization-method) and some [background info](https://stackoverflow.com/a/896383/7339946) – JSteward Mar 08 '18 at 16:46
  • `XmlSerializer` events are not implemented. Take a look at [How do you find out when you've been loaded via XML Serialization?](https://stackoverflow.com/q/1266547) and [In .Net/C# is the OnSerializing event fired for XmlSerializer.Serialize](https://stackoverflow.com/q/200386) for some workaround options. – dbc Mar 15 '18 at 00:14

1 Answers1

0

I think it is not possible with the current implementation of the Microsoft XmlSerializer.

Eric Ouellet
  • 10,996
  • 11
  • 84
  • 119