The below class is a contrived example. I would like the easiest way to serialize the 'isVisible' field to 'true' every single time it is serialized...regardless of what it is set to.
In the real code...this is a much larger object and has a few different constructors that are called from different points.
Right now, I can think of two ways.
Create an 'OnSerializing()' method that used reflection to set the property to 'true' during serialization. (I'm not actually sure if this will work...but it seems like it would).
In the method that initiates the serialization of the class...create all new items that have the isVisible as 'true' before serializing.
[DataContract] public class MapItem { public MapItem(bool isVisible) { this.isVisible = isVisible; } [DataMember] private readonly bool isVisible; public bool IsVisible => isVisible; }
Why do I need this? Basically, I need to load in these items and have them always be visible directly after a load. While running the application...the MapItems may become visible/invisible. Thus I want the XML to always have <isVisible>true</isVisible>
OR I want the value inside MapItem to be 'true' ONLY when deserializing.