English 中文(简体)
protobuf-net 收集现有档案
原标题:protobuf-net Merge collection to existing file

我利用Protobuf-net将收集资料编成文件。

我正在设法将另外几个项目合并到现有档案中。

现在我有:

[Test]
public void CanAppend()
{
    var list = new List<Person>();

    for (int i = 0; i < 1000; i++)
       list.Add(new Person {Name = i.ToString(), Age = i});

    using (var file = File.Create("person.bin"))
    {
       Serializer.Serialize<List<Person>>(file, list);
    }

    list.Clear();

    for (int i = 1000; i < 2000; i++)
       list.Add(new Person { Name = i.ToString(), Age = i });

    using (var file = File.OpenRead("person.bin"))
    {
       Serializer.Merge<List<Person>>(file, list);
    }

    using (var file = File.OpenRead("person.bin"))
    {
       list = Serializer.Deserialize<List<Person>>(file);
    }

    //Fails here    
    Assert.That(list.Count, Is.EqualTo(2000));
}

[ProtoContract]
class Person
{
   [ProtoMember(1)]
   public string Name { get; set; }

   [ProtoMember(2)]
   public int Age { get; set; }
}

但它没有工作。 任何想法?

最佳回答

deserialization<>_em>Operation(将溪流作为直线器改为现有物体)。 幸运的是,代用制序列只是附加性的,因此,你们都需要做的是:为附件(或以人工方式移至上游末),然后称作<编码>。

using (var file = File.Open("person.bin", FileMode.Append, FileAccess.Write)) {
    Serializer.Serialize(file, list);
}
问题回答

暂无回答




相关问题
WCF hosting in .NET compact framework

I would like to host a service on a WinCE device. The WinCE device is the host which can be accessed(control and data acquisition) by multiple clients (PC or WinCE) over serial port, TCP, USB etc. I ...

protobuf-net [de]serializing across assembly boundaries

I have a base class in one assembly and a large number of generated classes in another that inherit from the base class. Using protobuf-net (r282) to serialize a list of the base type fails when ...

Mismatched group tags detected in message - protobuf-net

I’m quite new to Silverlight. I’m working for a project which mainly depends on Serialization and Deserialization. Formerly, for WPF I was comfortable with Serializable classes. For silverlight, I ...

Serializing a List of objects using Protobuf-net

I ve been looking to do some binary serialization to file and protobuf-net seems like a well-performing alternative. I m a bit stuck in getting started though. Since I want to decouple the definition ...

DataContract serialization with protobuf-net r275

I just updated to r275 version and it doesn t seem to manage correctly DataContract classes any more By serializing this very simple class: [DataContract] public class ProtoData { [DataMember(...

热门标签