Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You can't detect the type in isolation, since the protobuf spec doesn't add any data to the stream for this; however, there are a number of ways of making this easy, depending on the context:</p> <ul> <li>a union type (as mentioned by Jon) covers a range of scenarios</li> <li>inheritance (protobuf-net specific) can be versatile - you can have a base-message type, and any number of concrete message types</li> <li>you can use a prefix to indicate the incoming type</li> </ul> <p>the last approach is actually very valuable in the case of raw TCP streams; this is <em>on the wire</em> identical to the union type, but with a different implementation; by deciding in advance that 1=Foo, 2=Bar etc (exactly as you do for the union type approach), you can use <code>SerializeWithLengthPrefix</code> to write (specifying the 1/2/etc as the field number), and the non-generic <code>TryDeserializeWithLengthPrefix</code> to read (this is under Serializer.NonGeneric in the v1 API, or on the TypeModel in the v2 API), you can provide a type-map that resolves the numbers back to types, and hence deserialize the correct type. And to pre-empt the question "why is this useful with TCP streams?" - because: in an ongoing TCP stream you <strong>need</strong> to use the <code>WithLengthPrefix</code> methods <strong>anyway</strong>, to avoid over-reading the stream; so you might as well get the type identifier for free!</p> <p>summary:</p> <ul> <li>union type: easy to implement; only down side is having to then check which of the properties is non-null</li> <li>inheritance: easy to implement; can use polymorphism or discriminator to handle "what now?"</li> <li>type prefix: a bit more fiddly to implement, but allows more flexibility, and has zero overhead on TCP streams</li> </ul>
    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.
    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