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 hiera开发者_如何转开发rchy (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.
Ê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!
精彩评论