如何序列化/反序列化属性的通用列表?

问题描述:

我有一个实体组件系统,每个实体都由一系列组件定义。一个组件是从基础组件派生的数据的一个组件对象 该应用程序包含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吗?还是别的吗?

回答我的问题,我只是增加了一个DataContract标志所有相关类和[数据成员]所有相关的属性

这需要自定义类的使用标志的[knowntype。

然后,我使用DatacontractSerialiser和FileStream对此进行了序列化