Note that there are some explanatory texts on larger screens.

plurals
  1. POProtobuf-net generic inheritance and closed constructed generic type
    primarykey
    data
    text
    <p>I have a fairly complex inheritance hierarchy including generics and we are trying to use protobuf .net for serialization purposes. Unfortunately it does not seem to be able to handle this case correctly. This is how the hierarchy looks like.</p> <pre><code> [System.Runtime.Serialization.DataContract] [ProtoBuf.ProtoInclude(1000, typeof(GenericBaseClass&lt;object&gt;))] [ProtoBuf.ProtoInclude(1001, typeof(GenericBaseClass&lt;string&gt;))] public abstract class BaseClass { public int BaseProperty1 { set; get; } public int BaseProperty2 { set; get; } public BaseClass() { } } [System.Runtime.Serialization.DataContract] [ProtoBuf.ProtoInclude(1002, typeof(GenericDerivedClass&lt;object&gt;))] [ProtoBuf.ProtoInclude(1003, typeof(GenericDerivedClass&lt;string&gt;))] public abstract class GenericBaseClass&lt;T&gt; : BaseClass { /// &lt;summary&gt; /// /// &lt;/summary&gt; [System.Runtime.Serialization.DataMember(Order = 5)] public T ResponseProperty { get; set; } public GenericBaseClass() { } } [System.Runtime.Serialization.DataContract] [ProtoBuf.ProtoInclude(1004, typeof(DerivedClass1))] [ProtoBuf.ProtoInclude(1005, typeof(DerivedClass2))] public abstract class GenericDerivedClass&lt;T&gt; : GenericBaseClass&lt;T&gt; { public int AdditionalProperty { get; set; } public GenericDerivedClass() { } } </code></pre> <p>Finally these classes are implemented by two closed constructed non generic classes</p> <pre><code> [System.Runtime.Serialization.DataContract] public class DerivedClass1 : GenericDerivedClass&lt;string&gt; { [System.Runtime.Serialization.DataMember(Order = 6)] public int DerivedClass1Property { set; get; } } [System.Runtime.Serialization.DataContract] public class DerivedClass2 : GenericDerivedClass&lt;object&gt; { [System.Runtime.Serialization.DataMember(Order = 7)] public int DerivedClass2Property { set; get; } } </code></pre> <p>I have written the following test method to serialize these and it is giving me error.</p> <pre><code> [TestMethod] public void SerializeDeserializeAndCompare() { DerivedClass2 i = new DerivedClass2() { BaseProperty1 = 1, BaseProperty2 = 2, DerivedClass2Property = 3, ResponseProperty = new Object() }; using (var file = System.IO.File.Create("test.bin")) { ProtoBuf.Serializer.Serialize(file, i); } using (var file = System.IO.File.OpenRead("test.bin")) { var o = ProtoBuf.Serializer.Deserialize&lt;DerivedClass2&gt;(file); } } </code></pre> <p>The error I am getting is </p> <p>ProtoBuf.ProtoException: A type can only participate in one inheritance hierarchy (CapitalIQ.DataGet.UnitTests.DataSetUnitTest+DerivedClass2) ---> System.InvalidOperationException: A type can only participate in one inheritance hierarchy</p> <p>Is this a limitation of protobuf .net or am I doing something incorrect. I am using r282 version.</p> <p>Thanks Shobhit</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload