使用 protobuf-net r275 进行 DataContract 序列化 [英] DataContract serialization with protobuf-net r275
问题描述
我刚刚更新到 r275 版本,它似乎不再正确管理 DataContract 类通过序列化这个非常简单的类:
I just updated to r275 version and it doesn't seem to manage correctly DataContract classes any more By serializing this very simple class:
[DataContract]
public class ProtoData
{
[DataMember(Order = 1)]
private long _id;
[DataMember(Order = 2)]
private string _firstName;
[DataMember(Order = 3)]
private string _lastName;
public long Id
{
get { return _id; }
set { _id = value; }
}
public string FirstName
{
get { return _firstName; }
set { _firstName = value; }
}
public string LastName
{
get { return _lastName; }
set { _lastName = value; }
}
public ProtoData(long id, string firstName, string lastName)
{
_id = id;
_firstName = firstName;
_lastName = lastName;
}
public ProtoData()
{
}
我得到只能处理数据契约类(以及此类的列表/数组)(错误处理 ProtoData)
I get Only data-contract classes (and lists/arrays of such) can be processed (error processing ProtoData)
推荐答案
真的吗?那是……奇怪;我原以为单元测试会发生如此重大的变化.您确定您使用的是正确的版本吗?有一个 2.0 版本(不包括 [DataContract]
支持,因为这是在 WCF 中,一个 3.0 扩展)和一个单独的 3.0 版本.您需要 3.0 版本 (NET30.zip
).
Really? that is.... odd; I would have expected the unit tests to spt such a breaking change. Are you sure you are using the right version? There is a 2.0 version (which doesn't include [DataContract]
support, since this is in WCF, a 3.0 extension) and a separate 3.0 version. You want the 3.0 version (NET30.zip
).
使用 r275/NET30 测试成功:
Tested successfully with r275/NET30:
static void Main() {
ProtoData pd = new ProtoData {
FirstName = "Marc",
LastName = "Gravell",
Id = 23354
}, clone;
using (MemoryStream ms = new MemoryStream()) {
Serializer.Serialize(ms, pd);
Console.WriteLine(ms.Length);
ms.Position = 0;
clone = Serializer.Deserialize<ProtoData>(ms);
}
Console.WriteLine(clone.FirstName);
Console.WriteLine(clone.LastName);
Console.WriteLine(clone.Id);
}
有输出:
19
Marc
Gravell
23354
这篇关于使用 protobuf-net r275 进行 DataContract 序列化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!