title: C# deserialize to dynamic class
body: <p>On my home automation system, to create a plugin with documentation I must use class with summary. </p>
  
  <p>My plugin talk to my xiaomi gateway to get informations about sensors. So there are different type of sensor like "sensor_ht", "magnet" or "motion". </p>
  
  <p>For each sensor, there are common proprieties and a data part that is different. I get status of each sensor in jon. </p>
  
  <p>For example :</p>
  
  
  
  <pre><code>{"cmd":"report","model":"magnet","sid":"","short_id":40805,"data":"
  {\"status\":\"close\"}"}
  {"cmd":"report","model":"sensor_ht","sid":"","short_id":40805,"data":"
  {\"voltage\":\"3000\",\"temperature\":\"2367\",\"humidity,\":\"5285\"}"}
  </code></pre>
  
  <p>So I create many class like this, each sensor is in a single file.</p>
  
  <p>sensor_ht.cs :</p>
  
  <pre><code>public class sensor_ht
  {
      public string model { get; set; } = "sensor_ht";
      public string sid { get; set; }
      public string battery_type { get; set; } = "CR2032";
      public int battery { get; set; }
      public sensor_ht.Report report { get; set; }
  }
  
  public class Report
  {
      public int voltage { get; set; }
      public string temperature { get; set; }
      public string humidity { get; set; }
  }
  </code></pre>
  
  <p>magnet.cs :</p>
  
  <pre><code>public class magnet
  {
      public string model { get; set; } = "sensor_ht";
      public string sid { get; set; }
      public string battery_type { get; set; } = "CR2032";
      public int battery { get; set; }
      public magnet.Report report { get; set; }
  }
  
  public class Report
  {
      public int voltage { get; set; }
      public string status { get; set; }
  }
  </code></pre>
  
  <p>And this is my report class :</p>
  
  <pre><code>public class Report
  {
  
          public string cmd { get; set; }
          public string model { get; set; }
          public string sid { get; set; }
          public int short_id { get; set; }
          public string token { get; set; }
          public string data { get; set; }
  }
  </code></pre>
  
  <p>After deserialize the report, I initialize the instance with :</p>
  
  <pre><code>dynamic d = System.Reflection.Assembly.GetExecutingAssembly().CreateInstance("XiaomiSmartHome.Equipement." + read.model);
  </code></pre>
  
  <p>But to create the data part I deserialize the data of the report with :</p>
  
  <pre><code>dynamic data2 = JsonConvert.DeserializeObject&lt;dynamic&gt;(read.data);
  </code></pre>
  
  <p>I want this data to be the right Report class. I tried :</p>
  
  <pre><code>d.report = data2;
  </code></pre>
  
  <p>But it sais that it's impossible to convert Newtonsoft.Json.Linq.JObject to XiaomiSmartHome.Equipement.Report</p>
  
  <p>So my way to handle this is right ? And how to deserialize the data part to the right report ?</p>
  
  <p>Thank !</p>