Shane Courtrille
Shane Courtrille

Reputation: 14107

protobuf-net throwing exception for generic base class

I have the following..

[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>
{
}

When I try to serialize a DateRange I get the following error..

ProtoBuf.ProtoException : A type can only participate in one inheritance hierarchy (DateRange) ----> System.InvalidOperationException : A type can only participate in one inheritance hierarchy

After spending some time in the source code I am pretty sure the problem is that DateRange and IntRange both techincally have a different parent since Range< DateTime> != Range< int>. So really I am not sure how we`re expected to handle generics.

Upvotes: 2

Views: 753

Answers (1)

Shane Courtrille
Shane Courtrille

Reputation: 14107

Ênded up taking the details from the issue Marc linked to and created this:

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

Kind of a pain but at least it works!

Upvotes: 1

Related Questions