使用 protobuf-net r275 进行 DataContract 序列化
我刚刚更新到 r275 版本,它似乎不再正确管理 DataContract 类 通过序列化这个非常简单的类:
[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 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()
{
}
I get Only data-contract classes (and lists/arrays of such) can be processed (error processing ProtoData)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
真的吗?那是……奇怪;我本来期望单元测试能够解决这样一个重大的变化。您确定您使用的是正确的版本吗?有一个 2.0 版本(不包括
[DataContract]
支持,因为这是 WCF 中的 3.0 扩展)和一个单独的 3.0 版本。您需要 3.0 版本 (NET30.zip
)。使用 r275/NET30 成功测试:
输出:
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
).Tested successfully with r275/NET30:
With output:
请尝试以下操作:
使用公共属性
public string LastName;
使用 [DataMember] 标记所有公共属性
Try the following:
Use public properties
public string LastName;
Mark all public properties with [DataMember]