title: Using .NET XmlSerializer with get properties and setter functions
body: <p>I'm trying to use <code>XmlSerializer</code> from C# to save out a class that has some values that are read by properties (the code being just a simple retrieval of field value) but set by setter functions (since there is a delegate called if the value changes). </p>
  
  <p>What I'm currently doing is this sort of thing. The intended use is to use the <code>InT</code> property to read the value, and use <code>SetInT</code> to set it. Setting it has side-effects, so a method is more appropriate than a property here. <code>XmlSerializationOnly_InT</code> exists solely for the benefit of the <code>XmlSerializer</code> (hence the name), and shouldn't be used by normal code.</p>
  
  <pre><code>class X
  {
      public double InT
      {
          get { return _inT; }
      }
  
      public void SetInT(double newInT)
      {
          if (newInT != _inT)
          {
              _inT = newInT;
              Changed();//includes delegate call; potentially expensive
          }
      }
  
      private double _inT;
  
      // not called by normal code, as the property set is not just a simple
      // field set or two.
      [XmlElement(ElementName = "InT")]
      public double XmlSerializationOnly_InT
      {
          get { return InT; }
          set { SetInT(value); }
      }
  }
  </code></pre>
  
  <p>This works, it's easy enough to do, and the XML file looks like you'd expect. It's manual labour though, and a bit ugly, so I'm only somewhat satisfied. What I'd really like is to be able to tell the XML serialization to read the value using the property, and set it using the setter function. Then I wouldn't need <code>XmlSerializationOnly_InT</code> at all.</p>
  
  <p>I seem to be following standard practise by distinguishing between property sets and setter functions in this way, so I'm sure I'm not the only person to have encountered this (though google suggests I might be). What have others done in this situation? Is there some easy way to persuade the <code>XmlSerializer</code> to handle this sort of thing better? If not, is there perhaps some other easy way to do it?</p>