Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>As with all attributes, the included type information on attributes apply to all closed types from the generic type definition. Thus, what you have actually defined (to protobuf-net) is:</p> <pre><code>BaseClass : GenericBaseClass&lt;object&gt; : GenericDerivedClass&lt;object&gt; : DerivedClass1 : DerivedClass2 : GenericDerivedClass&lt;string&gt; : DerivedClass1 : DerivedClass2 : GenericBaseClass&lt;string&gt; : GenericDerivedClass&lt;object&gt; : DerivedClass1 : DerivedClass2 : GenericDerivedClass&lt;string&gt; : DerivedClass1 : DerivedClass2 </code></pre> <p>As you can see, there are lots of duplicates - which is clearly confusing. Since attribute arguments can't use type parameters, this would leave the option of adding some kind of odd predicate mechanism, which is IMO pretty confusing. IMO, it would be a better idea to model this manually (removing the <code>ProtoInclude</code> attributes). I <em>suspect</em> your intended model is:</p> <pre><code>BaseClass : GenericBaseClass&lt;object&gt; : GenericDerivedClass&lt;object&gt; : DerivedClass2 : GenericBaseClass&lt;string&gt; : GenericDerivedClass&lt;string&gt; : DerivedClass1 </code></pre> <p>protobuf-net can work with that, but to <em>explain</em> the model requires "v2" and the <code>RuntimeTypeModel</code>:</p> <p>Note also that <code>object</code> is a bit of a problem for protobuf; protobuf-net can fake around it with the dynamic-type option, but that is... not ideal. It certainly can't serialize an <code>object</code>, so for the test I've substituted a string. Note also that <code>BaseProperty1</code>, <code>BaseProperty2</code> and <code>AdditionalProperty</code> are not currently marked for serialization, but can be trivially.</p> <p>Anyway:</p> <pre><code>RuntimeTypeModel.Default[typeof(BaseClass)] .AddSubType(10, typeof(GenericBaseClass&lt;object&gt;)) .AddSubType(11, typeof(GenericBaseClass&lt;string&gt;)); RuntimeTypeModel.Default[typeof(GenericBaseClass&lt;object&gt;)] .AddSubType(10, typeof(GenericDerivedClass&lt;object&gt;)); RuntimeTypeModel.Default[typeof(GenericBaseClass&lt;object&gt;)][5].DynamicType = true; // object! RuntimeTypeModel.Default[typeof(GenericDerivedClass&lt;object&gt;)] .AddSubType(10, typeof(DerivedClass2)); RuntimeTypeModel.Default[typeof(GenericBaseClass&lt;string&gt;)] .AddSubType(10, typeof(GenericDerivedClass&lt;string&gt;)); RuntimeTypeModel.Default[typeof(GenericDerivedClass&lt;string&gt;)] .AddSubType(10, typeof(DerivedClass1)); DerivedClass2 i = new DerivedClass2() { BaseProperty1 = 1, BaseProperty2 = 2, DerivedClass2Property = 3, ResponseProperty = "some string" }; 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>You don't <em>have</em> to use <code>RuntimeTypeModel.Default</code> - in fact, I recommend using (and caching) a separate type-model; but <code>Serializer.Serialize</code> points at the default model. If you create a custom model (<code>TypeModel.Create</code>) just store it somewhere and use the <code>Serialize</code> etc from there.</p>
    singulars
    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.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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