title: Deserialize runtime created class
body: <p>I'm triying to deserialize a runtime created type with C# and JSON.net but it throws a resolve type exception.</p>
  
  <p>I have an empty BaseElement class from where all the runtime created types inherit, all the types are guaranteed to be created before the deserialization process and I'm using the TypeNameHandling = TypeNameHandling.All setting for serialization and deserialization.</p>
  
  <p>The process of creating the classes is this (reduced to an example):</p>
  
  <pre><code>        AssemblyName asmName = new AssemblyName();
          asmName.Name = "RaspElementTypes";
          AssemblyBuilder asmBuild = Thread.GetDomain().DefineDynamicAssembly(asmName, AssemblyBuilderAccess.RunAndSave);
  
          ModuleBuilder modBuild = asmBuild.DefineDynamicModule("TypeModule", "RaspElementTypes.dll");   
  
          TypeBuilder tb = modBuild.DefineType("ClassInfo", TypeAttributes.Class, typeof(BaseElement));
  
          //Add properties, functions, etc
  
          Type finalType =  tb.CreateType();
          var item = (finalType)Activator.CreateInstance(tType);
  </code></pre>
  
  <p>And the serialization / deserialization is like this:</p>
  
  <pre><code>        //Data is a BaseElement[] array with the ClassInfo instances
  
          //Serialize
  
          JsonSerializerSettings settings = new JsonSerializerSettings { TypeNameHandling = TypeNameHandling.All };
          string res = JsonConvert.SerializeObject(Data, Formatting.Indented, settings);
  
          //Deserialize
  
          var deser = JsonConvert.DeserializeObject &lt;BaseElement[]&gt;(res, settings);
  </code></pre>
  
  <p>The exact error is this:</p>
  
  <p><em>An unhandled exception of type 'Newtonsoft.Json.JsonSerializationException' occurred in Newtonsoft.Json.dll
  Additional information: Error resolving type specified in JSON 'ClassInfo, RaspElementTypes'. Path '$values[0].$type'.</em></p>
  
  <p>Any idea on how to deserialize the runtime created types?</p>
  
  <p>Thanks.</p>