title: Type is an interface or abstract class and cannot be instantiated
body: <p>I will preface this by saying that I know what the problem is, I just don't know how to solve it.  I am communicating with a .NET SOA data layer that returns data as JSON. One such method returns an object that has several collections within it. The object basically looks like this:</p>
  
  <pre><code>{
    "Name":"foo",
    "widgetCollection":[{"name","foo"}, {"name","foo"},],
    "cogCollection": [{"name","foo"}, {"childCogs",&lt;&lt;new collection&gt;&gt;},],
  }
  </code></pre>
  
  <p>My class that represents this object looks like this:</p>
  
  <pre><code>public class SuperWidget : IWidget
  {
      public string Name { get; set; }
  
      public ICollection&lt;IWidget&gt; WidgetCollection { get; set; }
      public ICollection&lt;ICog&gt; CogCollection { get; set; }
  
      public SuperWidget()
      {
      }
  
      [JsonConstructor]
      public SuperWidget(IEnumerable&lt;Widget&gt; widgets, IEnumerable&lt;Cog&gt; cogs)
      {
          WidgetCollection = new Collection&lt;IWidget&gt;();
          CogCollection = new Collection&lt;ICog&gt;();
  
          foreach (var w in widgets)
          {
              WidgetCollection.Add(w);
          }
          foreach (var c in cogs)
          {
              CogCollection.Add(c);
          }
      }
  }
  </code></pre>
  
  <p>This constructor worked fine until the cogCollection added a child collection, and now I am getting the above error.  A concrete cog class looks like this:</p>
  
  <pre><code>[Serializable]
  public class Cog : ICog
  {
      public string name { get; set; }
  
      public ICollection&lt;ICog&gt; childCogs { get; set; }        
  }  
  </code></pre>
  
  <p>I don't want to change the collection to a concrete type because I am using IoC. Because I am using IoC I would really like to get away from the need to have the JsonConstructors that take concrete parameters, but I haven't figured out a way to do that. Any advice would be greatly appreciated!</p>
  
  <p><strong>Update:</strong></p>
  
  <p>Yuval Itzchakov's suggestion that this question is probably a duplicate is somewhat true (it seems).  In the post referenced, one of the answers down the page provides same solution that was provided here. I didn't notice that answer, since the OP's question was different then the one I had here.  My mistake.  </p>
  
  <p>-------my solution--------</p>
  
  <p>As I said: Matt's solution took a little bit of work but I got something setup that works for me.  The one thing I didn't like about his initial solution, were lines like these:</p>
  
  <pre><code> return objectType == typeof(ICog); 
  </code></pre>
  
  <p>Following this pattern you would need to have a JsonConverter for every abstract type that you receive over the wire. This is less than ideal in my situation, so I created a generic JsonConverter as such:</p>
  
  <pre><code>public class GenericJsonConverter&lt;T&gt;: JsonConverter, IBaseJsonConverter&lt;T&gt;
  {
      private readonly IUnityContainer Container;
      public GenericJsonConverter(IUnityContainer container)
      {
          Container = container;
      }
  
      public override bool CanConvert(Type objectType)
      {
          return objectType == typeof(T);
      }
  
      public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
      {
          var target = serializer.Deserialize&lt;Newtonsoft.Json.Linq.JObject&gt;(reader);
          var result = Container.Resolve&lt;T&gt;();
          serializer.Populate(target.CreateReader(), result);
          return result;
      }
  
      public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
      {
          serializer.Serialize(writer, value);
      }
  }
  </code></pre>
  
  <p>Then just before I deserialize my data, I do something like this:</p>
  
  <pre><code> var settings = new JsonSerializerSettings { TypeNameHandling = TypeNameHandling.All };
   settings.Converters.Add((JsonConverter)Container.Resolve&lt;IBaseJsonConverter&lt;ICog&gt;&gt;());
  </code></pre>
  
  <p>Protip: If you use Resharper, (JsonConverter) will give you a suspicious cast warning in this scenario. </p>
  
  <p>Hopefully someone else finds this useful down the road!</p>