简体   繁体   English

使用protobuf-net反序列化不同的列表

[英]Deserializing a different List with protobuf-net

I have a problem with deserialization Protobuf-net. 我有一个反序列化Protobuf-net的问题。 I serialize a List<> but when I deserialize the List<> returned is not identical to the first. 我序列化List<>但是当我反序列化时返回的List<>与第一个不同。 There is missing data. 缺少数据。 I do not understand why. 我不理解为什么。

I initialize my List<> in ctr 我在ctr中初始化List<>

public ctr()
{
    DateTime date = new DateTime(2012 , 12, 03); ;
    LkVisiteurIdDTO visitid= new LkVisiteurIdDTO(10, 11, 12);
    PurgeDateTimeDTO datetime= new PurgeDateTimeDTO(date, true);
    ContractProtoBuf proto = new ContractProtoBuf();

    for (int i = 0; i < 5; i++)
    {
        proto.m_Index = i + 1;
        proto.m_TechnicalKey = "m_TechnicalKey" + i;
        proto.m_LogicalKey = visitid;
        proto.m_PurgeTime = datetime;

        protoContractList.Add(proto);
    }

}

public byte[] serialization()
{
    MemoryStream ms = new MemoryStream();
    try
    {
        Serializer.Serialize(ms, protoContractList);
        arr = ms.ToArray();
        return arr;
    }
    catch
    {
        Console.WriteLine("La sérialisation protobuf a échoué");
        return null;
    }
    finally
    {
        ms.Close();
    }

}

public List<ContractProtoBuf> deserialization()
{
    MemoryStream ms = new MemoryStream(arr,false);
    try
    {             
        listeRetour = Serializer.Deserialize<List<ContractProtoBuf>>(ms);
        return (listeRetour);
    }
    catch (Exception e)
    {
        Console.WriteLine("La désérialisation protobuf a échoué");
        return null;
    }
    finally
    {
        ms.Close();
    }
}

So my question is how to have same result ? 所以我的问题是如何获得相同的结果?


Here is my structure 这是我的结构

    [ProtoContract]
public struct ContractProtoBuf
{
    [ProtoMember(1)]
    public int m_Index;

    [ProtoMember(2)]
    public string m_TechnicalKey;

    [ProtoMember(3)]
    public LkVisiteurIdDTO m_LogicalKey;

    [ProtoMember(4)]
    public PurgeDateTimeDTO m_PurgeTime;
 }

I create a list of ContractProtoBuf and I add data with values ​​"that do not have consequence." 我创建了一个ContractProtoBuf列表,并添加了“没有后果”的数据。

    public ctr()
    {
DateTime date = new DateTime(2012 , 12, 03); ;

for (int i = 0; i < 5; i++)
{
    LkVisiteurIdDTO visitid= new LkVisiteurIdDTO(10, 11, 12);
    PurgeDateTimeDTO datetime= new PurgeDateTimeDTO(date, true);
    ContractProtoBuf proto = new ContractProtoBuf();
    proto.m_Index = i + 1;
    proto.m_TechnicalKey = "m_TechnicalKey" + i;
    proto.m_LogicalKey = visitid;
    proto.m_PurgeTime = datetime;

    protoContractList.Add(proto);
}    

} }

I serialize my list that gets a Byte [] and when I deserialize the Byte [], the return list does not contain the same values ​​in the type PurgeDateTimeDTO and LkVisiteurIdDTO . 我序列化我的列表获取Byte [],当我反序列化Byte []时,返回列表不包含类型PurgeDateTimeDTO和LkVisiteurIdDTO中的相同值。

In the return list I find the dateTime {01/01/0001 0:00:00} and visitid {0,0,0} However m.Index and m_Technicalkey have good value 在返回列表中,我找到dateTime {01/01/0001 0:00:00}和visitid {0,0,0}但是m.Index和m_Technicalkey具有良好的价值

You don't show the DTOs, but if I assume that ContractProtoBuf is a class , then you are adding the same instance each time. 您没有显示DTO,但如果我假设ContractProtoBuf是一个class ,那么每次都要添加相同的实例。 Equally, you are adding the same visit each time. 同样,您每次都会添加相同的访问 Normally, I would expect these to be different, for example: 通常情况下,我希望这些不同,例如:

public ctr()
{
    DateTime date = new DateTime(2012 , 12, 03); ;

    for (int i = 0; i < 5; i++)
    {
        LkVisiteurIdDTO visitid= new LkVisiteurIdDTO(10, 11, 12);
        PurgeDateTimeDTO datetime= new PurgeDateTimeDTO(date, true);
        ContractProtoBuf proto = new ContractProtoBuf();
        proto.m_Index = i + 1;
        proto.m_TechnicalKey = "m_TechnicalKey" + i;
        proto.m_LogicalKey = visitid;
        proto.m_PurgeTime = datetime;

        protoContractList.Add(proto);
    }    
}

But as I say - it is impossible to say for sure without more information. 但正如我所说 - 如果没有更多信息,就不可能肯定地说。 If you can provide a complete (ie runnable ) example, I'll be happy to look further. 如果你能提供一个完整的 (即可运行的 )例子,我会很乐意进一步研究。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM