title: Serializing/deserializing with BinaryFormatter when class has implicit operator
body: <p>We are using BinaryFormatter to convert objects to bytes when transferring between a server and client.</p>
  
  <p>All classes that we need to transfer uses the <code>[Serializable]</code> attribute, and it normally works as it should. We also use our own SerializationBinder when deserializing, to map it to the correct object type and assembly.</p>
  
  <p>But we have a problem when serializing/deserializing any object that has an implicit operator.
  When deserializing it does this from the operator type and value, and thus potentially loose information.
  So for instance, a class with two properties and one implicit operator, it will try to deserialize the object from the implicit operator type, and the values from the two properties seems lost.</p>
  
  <p>How can I force it to behave normally just like any other class? Or am I missing something?</p>
  
  <h1>Update</h1>
  
  <p>When doing further investigation, it seems like a combination of a wrapper class implementing ISerializable and child property using an implicit operator returning certain types.</p>
  
  <p>example:</p>
  
  <pre><code>[Serializable]
      class Dto
      {
          public int MyNumber { get; set; }
  
          // The implicit operator returning an int will be used when deseralized instead of the complete object
          // If you change it to the operator returning a string, it will not be used at all.
          // Other types like decimal will also trigger the problem, and if multiple are present the int seems to be one that is used.
  
          public static implicit operator int(Dto dto) =&gt; dto.MyNumber;
          //public static implicit operator string(Dto dto) =&gt; dto.MyNumber.ToString();
          //public static implicit operator decimal(Dto dto) =&gt; (decimal)dto.MyNumber;
      }
  
      [Serializable]
      class DtoWrapper : ISerializable
      {
          public Dto Dto { get; set; }
  
          public DtoWrapper() { }
  
          public DtoWrapper(SerializationInfo info, StreamingContext context)
          {
              Dto = (Dto)info.GetValue(nameof(Dto), typeof(Dto));
          }
  
          public void GetObjectData(SerializationInfo info, StreamingContext context)
          {
              info.AddValue(nameof(Dto), Dto);
          }
      }
  </code></pre>
  
  <p>When first serializing and then deserializing an instance of the <code>DtoWrapper</code> class, there will be an error that the Dto property cannot be set from an integer.</p>