[英]how to serialise / de-serialise a Generic List of Properties?
我有一個實體組件系統,其中每個實體由一系列組件定義。 一個Component是從基礎Component Object派生的數據的一部分。該應用程序包含一千個實體的列表,我需要對其進行序列化。
即
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>
我意識到我對組件上每個屬性的值的序列化都做錯了,但是您是否會以其他方式解決這個問題? 我會更好地序列化為JSON嗎? 還是其他?
要回答我自己的問題,我只需將DataContract標志添加到所有相關的類中,並將[DataMember]添加到所有相關的屬性中
這要求對自定義類使用[knowntype]標志。
然后我使用DatacontractSerialiser和FileStream將此序列化
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.