【发布时间】:2013-12-11 18:02:40
【问题描述】:
protobuf-net 是否支持嵌套类?
属性类型名称和顺序是否正确?
如下代码,使用替代属性 XmlType/XmlElement 代替 ProtoContract/ProtoInclude
using System.Xml.Serialization;
[XmlType("Person")]
public class Person
{
[XmlElement(ElementName = "Display Name",Order = 1)]
public string Name { get; set; }
[XmlElement(ElementName = "Age", Order = 2)]
public byte Age { get; set; }
[XmlType("Person.Address")]
public class Address
{
[XmlElement(ElementName = "Street",Order = 1)]
// before I had Order=3 to be unique, but as Mark says
// the "Order only needs to be unique inside a single type"
public string Street { get; set; }
[XmlElement(ElementName = "ZIP",Order =2)]
public string Zip { get; set; }
}
}
更新,在我下面的回答中,我写了最后一个类,用 protobuf 实现了一个 ServiceStack 服务。
【问题讨论】:
-
应该可以 - 您看到问题了吗?如果是这样:究竟是什么?请注意,“Order”只需要在单个类型中是唯一的 - 它可以是 Address 中的 1 和 2,因为 Address 不与 Person 共享任何内容。
-
我还没有测试它。对不起,我在尝试之前问过。我同意地址类中的订单。顺便说一句,我准备在几个相关的问题中测试它,如果可以的话。 1. XmlType 的命名应该遵循一些规则? 2. ProtoContact 和 XmlType 属性有什么区别(如果有的话)?除了 Inheritance [ProtoInclude(7, typeof(SomeDerivedType)] 中的明显内容。BRB 与我的测试。
-
Protobuf-net 根本不使用名称,除非您使用 GetSchema。不同之处在于 XML 属性不会强制 protobuf 依赖项进入 DTO,但 protobuf 属性可以让您更具体地控制序列化。
-
马克,我更新了我的测试,在我对 wiki 的回答中。它工作正常。只有我对 ServiceStack 3.9.71 和 protobuf-net 版本有问题,如果你有时间,请查看my question there。