title: JSON.NET serialization of System.Drawing.Color with TypeNameHandling
body: <p>I want to serialize a <code>Dictionary&lt;string, object&gt;</code> that may contain <code>System.Drawing.Color</code> values or other types as values.  I create a serializer with <code>TypeNameHandling.Auto</code>, and this works for most classes, but not <code>Color</code>.</p>
  
  <p>Example code: (DotNetFiddle: <a href="https://dotnetfiddle.net/UvphQO" rel="nofollow">https://dotnetfiddle.net/UvphQO</a>)</p>
  
  <pre><code>public class Program
  {
      class A { }
      class B { }
  
      public static void Main()
      {
          Dictionary&lt;string, object&gt; dict = new Dictionary&lt;string, object&gt;();
          dict["Foo"] = new A();
          dict["Bar"] = new B();
          dict["Baz"] = new object();
          dict["Rainbow"] = Color.FromArgb(20, 20, 20, 20);
  
          var ser = new JsonSerializer
          {
              TypeNameHandling = TypeNameHandling.Auto
          };
          var w = new JsonTextWriter(Console.Out)
          {
              Formatting = Formatting.Indented
          };
          ser.Serialize(w, dict);
      }
  }
  </code></pre>
  
  <p>The resulting output:</p>
  
  <pre><code>{
    "Foo": {
      "$type": "Program+A, mi1i2eqo"
    },
    "Bar": {
      "$type": "Program+B, mi1i2eqo"
    },
    "Baz": {},
    "Rainbow": "20, 20, 20, 20"
  }
  </code></pre>
  
  <p>As expected, instances of <code>A</code> or <code>B</code> in the dictionary have the needed <code>$type</code> metadata for reconstruction.  But instances of <code>Color</code> do not.  When this json is deserialized, <code>dict["Rainbow"]</code> is a <code>System.String</code>.</p>
  
  <p>There are many ways I could work around this, but the preferred solution would be to figure out why the serializer is doing something seemingly incorrect with <code>$type</code> in this case.</p>