title: how to serialise / de-serialise a Generic List of Properties?
body: <p>I have an Entity Component System whereby each entity is defined by a series of components. A Component being a peice of data derived from a base Component Object
  The application contains a list of 1000's of Entities, which i need to serialise.</p>
  
  <p>i.e.</p>
  
  <pre><code>public class Entity{
      public Guid ID{get;set;}
      public Dictionary&lt;int,Component&gt; Components{get;set;}
  }
  
  public class Component{
      public int Type{get;set;}
  }
  
  public class PositionComponent : Component{
      public Vector2 Location{get;set;}    
  }
  public class AreaComponent : Component{
      public Vector2 Offset{get;set;}
      public Rectangle Area{get;set;}   
  }
  
  static void Main{
      var entities = new Dictionary&lt;Guid,Enity&gt;();
  
      var e = new Entity();
      e.id = Guid.NewID();
      e.Components = new Dictionary&lt;int,Component&gt;{
              //Add a series of Components Here
          };
  
      entities.Add(e.ID,e);
  }
  </code></pre>
  
  <p>now if i want to serialise this i'm not entirely sure where to start</p>
  
  <p>i have tried defining each item as a DataContract i.e. </p>
  
  <pre><code>[DataContract]
  public class ComponentContract
  {
      [DataMember(Name = "t")]
      public int Type { get; set; }
  
      [DataMember(Name = "p")]
      public List&lt;Tuple&lt;string, string&gt;&gt; Properties { get; set; }
  }
  [DataContract(Name = "Ent")]
  public class EntityContract
  {
      [DataMember(Name = "id")]
      public Guid ID { get; set; }
  
      [DataMember(Name = "comp")]
      public List&lt;ComponentContract&gt; Components { get; set; }
  }
  </code></pre>
  
  <p>and adding a Generic ToContract() Method to Component</p>
  
  <pre><code>public class Component{
      public int Type{get;set;}
  
      public ComponentContract ToContract()
      {
          var c = new ComponentContract();
          c.Type = Type.ID;
          c.Properties = new List&lt;Tuple&lt;string, string&gt;&gt;();
  
          foreach (var v in this.GetType().GetProperties()) {
              c.Properties.Add(new Tuple&lt;string, string&gt;(v.Name, this.GetType().GetProperty(v.Name).GetValue(this, null).ToString()));
          }
          return c;
  
      }
  }
  </code></pre>
  
  <p>however when i try to serialise this using DataContractSerialiser it is not returning much useful information </p>
  
  <pre><code>&lt;ManagerContract xmlns="http://schemas.datacontract.org/2004/07/Windows_Library.EntityComponentSystem.Managers" xmlns:i="http://www.w3.org/2001/XMLSchema-instance"&gt;
  &lt;Es xmlns:a="http://schemas.datacontract.org/2004/07/Windows_Library.EntityComponentSystem.Entity"&gt;
      &lt;a:Ent&gt;
          &lt;a:ccomp&gt;
              &lt;cc xmlns=""&gt;
                  &lt;p xmlns:b="http://schemas.datacontract.org/2004/07/System"/&gt;
                  &lt;t&gt;1&lt;/t&gt;
              &lt;/cc&gt;
              &lt;cc xmlns=""&gt;
                  &lt;p xmlns:b="http://schemas.datacontract.org/2004/07/System"/&gt;
                  &lt;t&gt;2&lt;/t&gt;
              &lt;/cc&gt;
              &lt;cc xmlns=""&gt;
                  &lt;p xmlns:b="http://schemas.datacontract.org/2004/07/System"/&gt;
                  &lt;t&gt;3&lt;/t&gt;
              &lt;/cc&gt;
          &lt;/a:ccomp&gt;
          &lt;a:id&gt;d047f8b6-cab0-4b04-81be-84a2380ef4f9&lt;/a:id&gt;
          &lt;a:type&gt;MovingObject&lt;/a:type&gt;
      &lt;/a:Ent&gt;
      &lt;a:Ent&gt;
          &lt;a:ccomp&gt;
              &lt;cc xmlns=""&gt;
                  &lt;p xmlns:b="http://schemas.datacontract.org/2004/07/System"/&gt;
                  &lt;t&gt;1&lt;/t&gt;
              &lt;/cc&gt;
              &lt;cc xmlns=""&gt;
                  &lt;p xmlns:b="http://schemas.datacontract.org/2004/07/System"/&gt;
                  &lt;t&gt;2&lt;/t&gt;
              &lt;/cc&gt;
              &lt;cc xmlns=""&gt;
                  &lt;p xmlns:b="http://schemas.datacontract.org/2004/07/System"/&gt;
                  &lt;t&gt;3&lt;/t&gt;
              &lt;/cc&gt;
          &lt;/a:ccomp&gt;
          &lt;a:id&gt;87f548cf-9292-4004-a492-d97f74ccd74b&lt;/a:id&gt;
          &lt;a:type&gt;MovingObject&lt;/a:type&gt;
      &lt;/a:Ent&gt;
      &lt;a:Ent&gt;
          &lt;a:ccomp&gt;
              &lt;cc xmlns=""&gt;
                  &lt;p xmlns:b="http://schemas.datacontract.org/2004/07/System"/&gt;
                  &lt;t&gt;1&lt;/t&gt;
              &lt;/cc&gt;
              &lt;cc xmlns=""&gt;
                  &lt;p xmlns:b="http://schemas.datacontract.org/2004/07/System"/&gt;
                  &lt;t&gt;2&lt;/t&gt;
              &lt;/cc&gt;
              &lt;cc xmlns=""&gt;
                  &lt;p xmlns:b="http://schemas.datacontract.org/2004/07/System"/&gt;
                  &lt;t&gt;3&lt;/t&gt;
              &lt;/cc&gt;
          &lt;/a:ccomp&gt;
          &lt;a:id&gt;8a08d24f-865e-4c08-a74f-5f6ed4add458&lt;/a:id&gt;
          &lt;a:type&gt;MovingObject&lt;/a:type&gt;
      &lt;/a:Ent&gt;
      &lt;a:Ent&gt;
          &lt;a:ccomp&gt;
              &lt;cc xmlns=""&gt;
                  &lt;p xmlns:b="http://schemas.datacontract.org/2004/07/System"/&gt;
                  &lt;t&gt;1&lt;/t&gt;
              &lt;/cc&gt;
              &lt;cc xmlns=""&gt;
                  &lt;p xmlns:b="http://schemas.datacontract.org/2004/07/System"/&gt;
                  &lt;t&gt;2&lt;/t&gt;
              &lt;/cc&gt;
              &lt;cc xmlns=""&gt;
                  &lt;p xmlns:b="http://schemas.datacontract.org/2004/07/System"/&gt;
                  &lt;t&gt;3&lt;/t&gt;
              &lt;/cc&gt;
          &lt;/a:ccomp&gt;
          &lt;a:id&gt;fd1c7d63-a1f7-46c4-87f0-8454bcb09600&lt;/a:id&gt;
          &lt;a:type&gt;MovingObject&lt;/a:type&gt;
      &lt;/a:Ent&gt;
  &lt;/Es&gt;
  &lt;/ManagerContract&gt;
  </code></pre>
  
  <p>I realise i'm doing something wrong with the seriailsation of the Value of each property on the component, but also would you approach this issue in a different way? would i be better serialising to JSON? or somthing else?</p>