今天我一直在使用protobuf-net,並且遇到了一個奇怪的情況。下面的代碼不會按預期反序列化。最後兩個反序列化嘗試成功,但它們不正確。反序列化的對象的IsEmpty設置爲true
,實際上它應設置爲false
。我已經能夠通過私人設置器來獲得序列化的屬性,但這個行爲是不正確的。它與鏈式默認構造函數有關嗎?protobuf-net沒有序列化與私人setter的C#屬性
class Program2
{
static void Main(string[] args)
{
var comp = new FooComparer();
// this deserializes fine. woot!
using (var ms = new MemoryStream())
{
Console.WriteLine("Serializing an empty Foo");
var args1 = new Foo();
Serializer.Serialize(ms, args1);
ms.Position = 0;
var result = Serializer.Deserialize(ms);
Console.WriteLine("Serialization successful: {0}", comp.Equals(args1, result));
Console.WriteLine();
}
// this deserializes incorrectly
using (var ms = new MemoryStream())
{
Console.WriteLine("Serializing a Foo with just a string");
var args1 = new Foo("576000BJ1");
Serializer.Serialize(ms, args1);
ms.Position = 0;
var result = Serializer.Deserialize(ms);
Console.WriteLine("Serialization successful: {0}", comp.Equals(args1, result));
Console.WriteLine();
}
// this deserializes incorrectly
using (var ms = new MemoryStream())
{
Console.WriteLine("Serializing a Foo with an int");
var args1 = new Foo(42);
Serializer.Serialize(ms, args1);
ms.Position = 0;
var result = Serializer.Deserialize(ms);
Console.WriteLine("Serialization successful: {0}", comp.Equals(args1, result));
Console.WriteLine();
}
Console.WriteLine("Got dat 190% serialization");
}
}
[ProtoContract]
class Foo
{
private Foo(bool isEmpty, string fooString, int? fooInt)
{
this.IsEmpty = isEmpty;
this.FooString = fooString;
this.FooInt = fooInt;
}
public Foo() : this(true, null, null) { }
public Foo(string foo) : this(false, foo, null) { }
public Foo(int foo) : this(false, null, foo) { }
[ProtoMember(10)] public bool IsEmpty { get; private set; }
[ProtoMember(20)] public string FooString { get; private set; }
[ProtoMember(30)] public int? FooInt { get; private set; }
}
class FooComparer : IEqualityComparer
{
public bool Equals(Foo x, Foo y)
{
return (x == null && y == null) ||
(x != null && y != null &&
x.IsEmpty == y.IsEmpty &&
String.Equals(x.FooString, y.FooString, StringComparison.Ordinal) &&
x.FooInt == y.FooInt);
}
public int GetHashCode(Foo obj) { return 1; } // don't care about this
}
編輯: 我使用.NET 3.5的protobuf 2.0.0.666