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

时间:2014-08-31 09:42:00

标签: c# serialization

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

并向Component

添加Generic 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吗?还是别的什么?

1 个答案:

答案 0 :(得分:0)

回答我自己的问题,我只是将DataContract标志添加到所有相关类,并将[DataMember]添加到所有相关属性

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

然后使用DatacontractSerialiser和FileStream

对其进行序列化