title: Dynamic Types with MongoDB Driver 2.2 - How to not serialize the _t type Discriminator?
body: <p>I have two .Net (C#) applications that are passing "messages" via Mongo.  One is a <em>Writer</em>, the other a <em>Reader</em>.  The <strong>_t</strong> type discriminator is causing me issues with deserialization.</p>
  
  <p>This is as simplified version of the common class they are using to communicate with:</p>
  
  <pre><code>public class MyMessage {
      public long Id {get; set;}
      public string MessageText {get; set;}
      public dynamic OriginalObject{get; set;}
  }
  </code></pre>
  
  <p>The <em>Writer</em> app is actually creating instances of <em>MyMessage</em> by mapping over values from some other type that will be unknown to the <em>Reader</em> app.  It maps out the known common elements, and then assigns the entire original object to the dynamic <em>OriginalObject</em> property.</p>
  
  <pre><code>public MyMessage CreateMessage(SomeOtherType originalMsg) {
      return new MyMessage {
          MessageText = originalMsg.SomeField,
          OriginalObject = originalMsg            
      };
  }
  </code></pre>
  
  <p>When the .Net Mongo Driver serializes <em>MyMessage</em>, it adds the <strong>_t</strong> type discriminator to the <em>OriginalObject</em> sub document.   </p>
  
  <p>However, the <em>Reader</em> app does not have reference to all of the types that <em>Writer</em> does.  When the <em>Reader</em> application attempts to deserialize <em>MyMessage</em>, it throws an <code>Unknown discriminator value</code> error whenever <em>OriginalObject</em> is a type it doesn't have.</p>
  
  <p>I've read that the <strong>_t</strong> type discriminator is added whenever the actual type doesn't match the nominal type.  And that makes sense in a true polymorphic, strongly typed scenario.  But now that C# has dynamic typing, I would like to use it.</p>
  
  <p>Also, if the instance of <em>MyMessage.OriginalMessage</em> is a purely anonymous type, it works perfectly.  No <strong>_t</strong> type discriminator is written.  The <em>Reader</em> app happily deserializes it as a dynamic (expando) object and everything works. It's only when the instance of <em>OriginalMessage</em> is some strong type that I have this issue.</p>
  
  <p><strong>How do I tell the Mongo Driver not to add the _t type discriminator for dynamic types?</strong>
  I'm also interested in other solutions/work arounds, but simply excluding the <strong>_t</strong> would be my preferred approach.</p>
  
  <p>.Net Framework 4.6.1, MongoDB.Driver 2.2.3</p>
  
  <p>More sample code:</p>
  
  <pre><code>public void SaveMessages()
  {
      var message1 = new MyMessage { 
          Id=1, 
          MessageText="Anonymous Message", 
          OriginalMessage=new{ Field1 = "f1", Field2 = "f2" }
      };
      _collection.InsertOne(message1);  // no _t since OriginalMessage is purely anonymous
  
      var message2 = new MyMessage { 
          Id=2, 
          MessageText="Strong Message", 
          OriginalMessage=new SomeOtherType{Prop1="p1", Prop2="p2"}
      };
      _collection.InsertOne(message2); // has _t since SomeOtherType is not "dynamic"
  }
  </code></pre>