2011-03-26 96 views
3

嘗試使用protobuf-net合併保存和恢復對象。 我可以得到簡單的對象來工作,但我無法將集合屬性對象獲取到合併。 任何來自protobuf網專家的幫助表示讚賞。使用protobuf-net合併收集屬性對象

working code: 

using System; 
using System.Collections.Generic; 
using System.IO; 
using System.Runtime.Serialization; 
using ProtoBuf; 
class Program 
{ 
    static void Main(string[] args) 
    { 
     ChildTest(); 
     ParentTest(); 

     Console.Write("Press any key to continue..."); 
     Console.ReadKey(); 
    } 

    private static void ChildTest() 
    { 
     Console.WriteLine("ChildTest:"); 
     var child = new Child() {Name = "OriginalName"}; 

     var stream = new MemoryStream(); 
     Serializer.Serialize(stream, child); 
     var tempChild = Serializer.Deserialize<Child>(new MemoryStream(stream.ToArray())); 
     tempChild.Name = "MergedName"; 

     var stream1 = new MemoryStream(); 
     Serializer.Serialize(stream1, tempChild); 
     child = Serializer.Deserialize<Child>(new MemoryStream(stream1.ToArray())); 
     Console.WriteLine(child.Name + " - MergedName as expected"); 
    } 

    private static void ParentTest() 
    { 
     Console.WriteLine("ParentTest:"); 
     var child = new Child() {Name = "OriginalName"}; 
     var parent = new Parent {Items = new List<Child>(1)}; 
     parent.Items.Add(child); 

     var stream = new MemoryStream(); 
     Serializer.Serialize(stream,parent); 
     var tempParent = Serializer.Deserialize<Parent>(new MemoryStream(stream.ToArray())); 
     tempParent.Items[0].Name = "MergedName"; 

     var stream1 = new MemoryStream(); 
     Serializer.Serialize(stream1, tempParent); 
     parent = Serializer.Merge(new MemoryStream(stream1.ToArray()), parent); 
     Console.WriteLine(parent.Items[0].Name + " - MergedName expected here"); 

     Parent nullParent = null; 
     nullParent = Serializer.Merge(new MemoryStream(stream1.ToArray()), nullParent); 
     Console.WriteLine(nullParent.Items[0].Name+ " - MergedName as expected for null"); 
    } 
} 

[ProtoContract] //, ProtoInclude(2, typeof(Child[])) 
public partial class Parent : ISerializable 
{ 
    public Parent() { } 

    [ProtoMember(1)] 
    public List<Child> Items { get; set; } 

    protected Parent(SerializationInfo info, StreamingContext context) 
    { 
     Serializer.Merge<Parent>(info, this); 
    } 
    public void GetObjectData(SerializationInfo info, StreamingContext context) 
    { 
     Serializer.Serialize(info, this); 
    } 
} 

[ProtoContract] 
public partial class Child : ISerializable 
{ 
    public Child() { } 

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

    protected Child(SerializationInfo info, StreamingContext context) 
    { 
     Serializer.Merge<Child>(info, this); 
    } 
    public void GetObjectData(SerializationInfo info, StreamingContext context) 
    { 
     Serializer.Serialize(info, this); 
    } 
} 

顯然這不能用protobuf網, - 有什麼辦法做我想做的事嗎?即覆蓋重複的屬性?另一個圖書館呢?

回答

3

這是預期的功能;合併一個單數屬性確實會用合併流中的值覆蓋屬性;然而,對於重複的屬性(列表,數組等),行爲(按照更廣泛的「protobuf」系列實現)是將來自合併流的附加對象附加到列表。

你會發現:

Console.WriteLine(parent.Items[0].Name); // prints OriginalName 
    Console.WriteLine(parent.Items[1].Name) // prints MergedName 

具體而言,它做一個項目,通過項目的位置合併。

+1

謝謝馬克,我希望能直接從你那裏得到答案。有沒有辦法做我想做的事情?即覆蓋重複的屬性。 – 2011-03-28 12:32:56