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();
}
}
}
问问题
1137 次
1 回答
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; }
于 2012-09-05T12:42:00.893 回答