2012-09-04 54 views
-3
using System; 
using System.Collections.Generic; 
using System.IO; 
using System.Runtime.Serialization.Formatters.Binary; 

namespace test_protosharp 
{ 
    [Serializable] 
    [ProtoBuf.ProtoContract] 
    public class MyClass 
    { 
     [ProtoSharp.Core.Tag(1)] 
     [ProtoBuf.ProtoMember(1)] 
     public int MessageType { get; set; } 

     [ProtoSharp.Core.Tag(2)] 
     [ProtoBuf.ProtoMember(2)] 
     public string Message { get; set; } 
    } 

    class Program 
    { 
     private static List<MyClass> _forSerialize; 

     static void Main() 
     { 
      _forSerialize = new List<MyClass> 
           { 
            new MyClass {MessageType = 0, Message = "Test1"}, 
            new MyClass {MessageType = 1, Message = "Test2"}, 
            new MyClass {MessageType = 2, Message = "Test3"}, 
            new MyClass {MessageType = 3, Message = "Test4"} 
           }; 

      // Test BinaryFormatter Serializer 
      using (Stream fs = File.Create("test.bin")) 
      { 
       BinaryFormatter bin = new BinaryFormatter(); 
       bin.Serialize(fs, _forSerialize); 
      } 

      using (Stream fs = File.OpenRead("test.bin")) 
      { 
       BinaryFormatter bin = new BinaryFormatter(); 
       _forSerialize = (List<MyClass>)bin.Deserialize(fs); 
      } 

      if (_forSerialize.Count == 4) 
       Console.WriteLine("BinaryFormatter serializer work"); 

      // Test protobuf-net Serializer 
      using (FileStream fs = File.Create("test.protobuf-net")) 
       ProtoBuf.Serializer.Serialize(fs, _forSerialize); 

      using (FileStream fs = File.OpenRead("test.protobuf-net")) 
       _forSerialize = ProtoBuf.Serializer.Deserialize<List<MyClass>>(fs); 

      if (_forSerialize.Count == 4) 
       Console.WriteLine("protobuf-net serializer work"); 

      // Test ProtoSharp Serializer 
      using (FileStream fs = File.Create("test.ProtoSharp")) 
       ProtoSharp.Core.Serializer.Serialize(fs, _forSerialize); 

      using (FileStream fs = File.OpenRead("test.ProtoSharp")) 
       _forSerialize = ProtoSharp.Core.Serializer.Deserialize<List<MyClass>>(fs); 

      if (_forSerialize.Count != 4) 
       Console.WriteLine("ProtoSharp serializer NOT work");    

      Console.ReadLine(); 
     } 
    } 
} 

回答

1

protosharp不支持列表作爲根對象;您需要將其包裝起來:

public class SomeWrapper 
{ 
    private readonly List<MyClass> items = new List<MyClass>(); 
    [ProtoSharp.Core.Tag(1)] 
    public List<MyClass> Items { get { return items; } } 
} 
... 
var tmp = new SomeWrapper(); 

tmp.Items.AddRange(_forSerialize); 
using (FileStream fs = File.Create("test.ProtoSharp")) 
    ProtoSharp.Core.Serializer.Serialize(fs, tmp); 

using (FileStream fs = File.OpenRead("test.ProtoSharp")) 
    _forSerialize = ProtoSharp.Core.Serializer.Deserialize<SomeWrapper>(fs).Items; 

if (_forSerialize.Count == 4) 
    Console.WriteLine("ProtoSharp serializer work"); 

這應該與protobuf-net輸出相同(以字節爲單位)。

然而,我會建議(可能不是非常虛心)protobuf-net做同樣的工作,但是已經被大量精煉和優化,遠遠超出了protosharp提供的,另外還支持許多更多的場景。

-

編輯:有趣的是,它們是不相同輸出; protosharp有2個額外的字節...我會看看我是否可以找出原因......編輯:啊,這只是零默認行爲;不要擔心這一點。有關信息,如果您按如下方式製作MessageType,則兩個基於protobuf的串行器的輸出是100%相同的(如您所希望和預期的那樣):

[ProtoSharp.Core.Tag(1)] 
[ProtoBuf.ProtoMember(1, IsRequired = true)] 
public int MessageType { get; set; }