带循环引用的protobuf-net v2

ab-*_*ols 5 c# protobuf-net

据我所知,从v2开始的protobuf-net支持引用。

我目前正在尝试在可能发生如下循环引用的数据结构中使用它:

[ProtoContract]
internal class WaypointDatabase
{
  [ProtoMember(1)]
  public List<Waypoint> Waypoints { get; set; }
}

[ProtoContract(AsReferenceDefault = true)]
internal class Waypoint
{
  [ProtoMember(1)]
  public string Ident { get; set; }

  [ProtoMember(2)]
  public List<Route> Routes { get; set; }
}

[ProtoContract]
internal class Route
{
  [ProtoMember(1)]
  public Waypoint PreviousWaypoint { get; set; }

  [ProtoMember(2)]
  public Waypoint NextWaypoint { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

因此,使用protobuf-net序列化的主要对象是的实例WaypointDatabase。该对象包含Waypoints一个列表,Routes该列表Waypoints再次指向。

我要使用此数据结构的原因是,某些航路点未包含在路径中/未由路径引用。因此,我需要此路标数据库来跟踪所有路标,无论它们是否在现有路线内。

现在,当我尝试序列化WaypointDatabaseprotobuf-net 的实例时,我得到一个StackOverflowException,但我不明白为什么,因为我将该Waypoint类标记为,AsReferenceDefault并且我认为这将确保protobuf-net仅在使用引用时才存储该引用。再次(并且这种方式不应无限地遵循循环参考)。

我究竟做错了什么?

在此先感谢您的提示

安德烈亚斯

PS:我正在使用protobuf-net的2.0.0.668版本,以防与特定版本相关。