2014-08-31 186 views
0

我有一個實體組件系統,每個實體都由一系列組件定義。一個組件是從基礎組件派生的數據的一個組件對象 該應用程序包含1000個實體的列表,我需要序列化。如何序列化/反序列化屬性的通用列表?

public class Entity{ 
    public Guid ID{get;set;} 
    public Dictionary<int,Component> 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<Guid,Enity>(); 

    var e = new Entity(); 
    e.id = Guid.NewID(); 
    e.Components = new Dictionary<int,Component>{ 
      //Add a series of Components Here 
     }; 

    entities.Add(e.ID,e); 
} 
現在,如果我想這個連載我不完全知道從哪裏開始

我試圖定義每個項目作爲DataContract即

[DataContract] 
public class ComponentContract 
{ 
    [DataMember(Name = "t")] 
    public int Type { get; set; } 

    [DataMember(Name = "p")] 
    public List<Tuple<string, string>> Properties { get; set; } 
} 
[DataContract(Name = "Ent")] 
public class EntityContract 
{ 
    [DataMember(Name = "id")] 
    public Guid ID { get; set; } 

    [DataMember(Name = "comp")] 
    public List<ComponentContract> Components { get; set; } 
} 

向組件添加通用ToContract()方法

public class Component{ 
    public int Type{get;set;} 

    public ComponentContract ToContract() 
    { 
     var c = new ComponentContract(); 
     c.Type = Type.ID; 
     c.Properties = new List<Tuple<string, string>>(); 

     foreach (var v in this.GetType().GetProperties()) { 
      c.Properties.Add(new Tuple<string, string>(v.Name, this.GetType().GetProperty(v.Name).GetValue(this, null).ToString())); 
     } 
     return c; 

    } 
} 

然而,當我嘗試這個連載使用DataContractSerialiser它沒有返回很多有用的信息

<ManagerContract xmlns="http://schemas.datacontract.org/2004/07/Windows_Library.EntityComponentSystem.Managers" xmlns:i="http://www.w3.org/2001/XMLSchema-instance"> 
<Es xmlns:a="http://schemas.datacontract.org/2004/07/Windows_Library.EntityComponentSystem.Entity"> 
    <a:Ent> 
     <a:ccomp> 
      <cc xmlns=""> 
       <p xmlns:b="http://schemas.datacontract.org/2004/07/System"/> 
       <t>1</t> 
      </cc> 
      <cc xmlns=""> 
       <p xmlns:b="http://schemas.datacontract.org/2004/07/System"/> 
       <t>2</t> 
      </cc> 
      <cc xmlns=""> 
       <p xmlns:b="http://schemas.datacontract.org/2004/07/System"/> 
       <t>3</t> 
      </cc> 
     </a:ccomp> 
     <a:id>d047f8b6-cab0-4b04-81be-84a2380ef4f9</a:id> 
     <a:type>MovingObject</a:type> 
    </a:Ent> 
    <a:Ent> 
     <a:ccomp> 
      <cc xmlns=""> 
       <p xmlns:b="http://schemas.datacontract.org/2004/07/System"/> 
       <t>1</t> 
      </cc> 
      <cc xmlns=""> 
       <p xmlns:b="http://schemas.datacontract.org/2004/07/System"/> 
       <t>2</t> 
      </cc> 
      <cc xmlns=""> 
       <p xmlns:b="http://schemas.datacontract.org/2004/07/System"/> 
       <t>3</t> 
      </cc> 
     </a:ccomp> 
     <a:id>87f548cf-9292-4004-a492-d97f74ccd74b</a:id> 
     <a:type>MovingObject</a:type> 
    </a:Ent> 
    <a:Ent> 
     <a:ccomp> 
      <cc xmlns=""> 
       <p xmlns:b="http://schemas.datacontract.org/2004/07/System"/> 
       <t>1</t> 
      </cc> 
      <cc xmlns=""> 
       <p xmlns:b="http://schemas.datacontract.org/2004/07/System"/> 
       <t>2</t> 
      </cc> 
      <cc xmlns=""> 
       <p xmlns:b="http://schemas.datacontract.org/2004/07/System"/> 
       <t>3</t> 
      </cc> 
     </a:ccomp> 
     <a:id>8a08d24f-865e-4c08-a74f-5f6ed4add458</a:id> 
     <a:type>MovingObject</a:type> 
    </a:Ent> 
    <a:Ent> 
     <a:ccomp> 
      <cc xmlns=""> 
       <p xmlns:b="http://schemas.datacontract.org/2004/07/System"/> 
       <t>1</t> 
      </cc> 
      <cc xmlns=""> 
       <p xmlns:b="http://schemas.datacontract.org/2004/07/System"/> 
       <t>2</t> 
      </cc> 
      <cc xmlns=""> 
       <p xmlns:b="http://schemas.datacontract.org/2004/07/System"/> 
       <t>3</t> 
      </cc> 
     </a:ccomp> 
     <a:id>fd1c7d63-a1f7-46c4-87f0-8454bcb09600</a:id> 
     <a:type>MovingObject</a:type> 
    </a:Ent> 
</Es> 
</ManagerContract> 

我知道我做錯了與組件上的每個屬性值的seriailsation,而且還會你以不同的方式處理這個問題?我會更好地序列化爲JSON嗎?還是別的嗎?

回答

0

回答我的問題,我只是增加了一個DataContract標誌所有相關類和[數據成員]所有相關的屬性

這需要自定義類的使用標誌的[knowntype。

然後,我使用DatacontractSerialiser和FileStream對此進行了序列化