【发布时间】:2015-08-28 13:54:47
【问题描述】:
我需要使用 protobuf-net serialize/deserialize 类。对于我的类的某些属性,我需要定义一个默认值。我通过设置属性的值来做到这一点。在某些情况下,此默认值会覆盖 protobuf 数据中的值。
代码示例:
public class Program
{
static void Main(string[] args)
{
var target = new MyClass
{
MyBoolean = false
};
using (var stream = new MemoryStream())
{
Serializer.Serialize(stream, target);
stream.Position = 0;
var actual = Serializer.Deserialize<MyClass>(stream);
//actual.MyBoolean will be true
}
}
}
[ProtoContract(Name = "MyClass")]
public class MyClass
{
#region Properties
[ProtoMember(3, IsRequired = false, Name = "myBoolean", DataFormat = DataFormat.Default)]
public Boolean MyBoolean { get; set; } = true;
#endregion
}
反序列化数据后,MyBoolean 的值为 true。
如何解决此问题?
【问题讨论】:
标签: c# protobuf-net