title: How to make readonly structs XML serializable?
body: <p>I have an immutable struct with only one field:</p>
  
  <pre><code>struct MyStruct
  {
      private readonly double number;
  
      public MyStruct(double number)
          =&gt; this.number = number;
  }
  </code></pre>
  
  <p>And I want this to be able to get serialized/deserialized by:</p>
  
  <ul>
  <li>Data contract serializer</li>
  <li>Binary formatter</li>
  <li>XML serializer (edit: forgotten in the original question)</li>
  <li>Json.NET (without adding Json.NET as a dependency)</li>
  </ul>
  
  <p>So the struct becomes this:</p>
  
  <pre><code>[Serializable]
  struct MyStruct : ISerializable, IXmlSerializable
  {
      private readonly double number;
  
      public MyStruct(double number)
          =&gt; this.number = number;
  
      private MyStruct(SerializationInfo info, StreamingContext context)
          =&gt; this.number = info.GetDouble(nameof(this.number));
  
      void ISerializable.GetObjectData(SerializationInfo info, StreamingContext context)
          =&gt; info.AddValue(nameof(this.number), this.number);
  
      XmlSchema IXmlSerializable.GetSchema() =&gt; null;
  
      void IXmlSerializable.ReadXml(XmlReader reader)
      {
          // Necessary evil
          reader.Read();
          this = new MyStruct(double.Parse(reader.Value, CultureInfo.InvariantCulture));
      }
  
      void IXmlSerializable.WriteXml(XmlWriter writer)
          =&gt; writer.WriteString(this.number.ToString(CultureInfo.InvariantCulture));
  }
  </code></pre>
  
  <p>Because:</p>
  
  <ul>
  <li><code>[Serializable]</code> is required by the binary formatter.</li>
  <li>Json.NET honors both <code>[DataContract]</code> and <code>ISerializable</code>.</li>
  <li><code>[DataContract]</code> and <code>ISerializable</code> can't be used together.</li>
  <li>Luckily, <code>IXmlSerializer</code> is supported by the data contract serializer.</li>
  </ul>
  
  <p>C# 7.2 introduces the <code>readonly</code> modifier for structs and MyStruct, being an immutable struct seems like an ideal candidate for this.</p>
  
  <p>The problem is that <code>IXmlSerializable</code> interface requires the ability to mutate <code>MyStruct</code>. That's what we did above, assigning to <code>this</code> in <code>IXmlSerializable.ReadXml</code> implementation.</p>
  
  <pre><code>readonly struct MyStruct : IXmlSerializable
  {
      // ...
      void IXmlSerializable.ReadXml(XmlReader reader)
      {
          // No longer works since "this" is now readonly.
          reader.Read();
          this = new MyStruct(double.Parse(reader.Value, CultureInfo.InvariantCulture));
      }
      // ...
  }
  </code></pre>
  
  <p>I tried cheating via reflection but <code>FieldInfo.SetValue</code> boxes the value, and <code>FieldInfo.SetValueDirect</code> requires a <code>TypedReference</code>, which I can't obtain since <code>__makeref</code> is also forbidden when <code>this</code> is read-only.</p>
  
  <p>So what are ways that would allow MyStruct to get serialized by the XML serializer?</p>
  
  <p>I should also mention that I do not care what the output XML looks like, I don't really need the fine grained control provided by the <code>IXmlSerializable</code> interface. I only need to make MyClass consistently serializable using the serializers I listed.</p>