title: C# MongoDB driver serialization - Working with binaries
body: <p>I've got a "complex" class which contains a property i want to serialized as a binary :</p>
  
  <pre><code>public class MyClassA{
    public MyClassB myClassB {get;set;}
    public MyClassB myClassC {get;set;}
    [BsonIgnore]
    public MyClassB myClassD {get;set;}
    private byte[] myClassDBin {get;set}
  }
  </code></pre>
  
  <p>I'd like to keep in RAM only the myClassD object but I would also like to serialize only the myClassDBin object. First condition is easy to achieve in the custructor :</p>
  
  <pre><code>public class MyClassA{
    public MyClassB myClassB {get;set;}
    public MyClassB myClassC {get;set;}
    [BsonIgnore]
    public MyClassB myClassD {get;set;}
    private byte[] myClassDBin {get;set}
  
    [BsonConstructor]
    public MyClassA(MyClassB myClassB, MyClassC myClassC, MyClassDBin[] myClassDBin)
    {
       this.myClassB = myClassB;
       this.myClassC = myClassC;
       this.myClassDBin = myClassBin;
       if(this.myClassDBin != null)
       {
          using(MemoryStream stream = new MemoryStream(this.myClassDBin))
          {
             this.myClassD = myClassD.Load(stream);//custom serializer
          }
       }
       this.myClassDBin = null; //Keep only one object in RAM
    }
  }
  </code></pre>
  
  <p>That works really fine for deserialization, but what about serialization ? If I want myClassDBin to be serialized, I need it to be not null all the time. Do I need to implement a custom serializer ? It could be a long task as the classes myClassB/C/D are large classes, it was easy with the automapper.... Is there an interface just like with XML we can implement to occur just before serialization ?</p>