【问题标题】:protobuf-net throwing exception for generic base class通用基类的 protobuf-net 抛出异常
【发布时间】:2011-09-12 16:51:34
【问题描述】:

我有以下..

[ProtoContract, ProtoInclude(50, typeof(DateRange)), ProtoInclude(51, typeof(IntRange))]
public class Range<T> : IEquatable<Range<T>>, IEquatable<OpenRange<T>> where T: struct, IComparable<T>
{
   [ProtoMember(1)]
   public T Start { get; set; }
   [ProtoMember(2)]
   public T End { get; set; }
}

[ProtoContract]
public class DateRange : Range<DateTime>
{
}

[ProtoContract]
public class IntRange : Range<int>
{
}

当我尝试序列化 DateRange 时,出现以下错误..

ProtoBuf.ProtoException : 一个类型只能参与一个继承层次结构 (DateRange) ----> System.InvalidOperationException : 一个类型只能参与一个继承层次

在源代码中花了一些时间之后,我很确定问题是 DateRange 和 IntRange 从技术上讲都有一个不同的父级,因为 Range!= Range。所以我真的不确定我们应该如何处理泛型。

【问题讨论】:

标签: protobuf-net


【解决方案1】:

我从 Marc 链接到的问题中获取了详细信息并创建了这个:

RuntimeTypeModel.Default[typeof (Range<DateTime>)]
   .AddSubType(50, typeof (DateRange));
RuntimeTypeModel.Default[typeof(Range<int>)]
   .AddSubType(50, typeof(IntRange));

有点痛苦,但至少它有效!

【讨论】:

  • 酷 - 我会看看我能做些什么来推断,因为它不断出现
  • 文档会为我做这件事.. 除了这里的内容之外,我在 ProtoInclude 上找不到任何东西 :)
猜你喜欢
  • 1970-01-01
  • 2021-10-20
  • 1970-01-01
  • 2020-11-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多