Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You cannot just get type of packed class from message, but you can pass some type identifier along with serialized data. </p> <p>This is how it can be done in generic way for interface serialization.</p> <pre><code>class InterfaceSerializer&lt;T&gt; : MessagePackSerializer&lt;T&gt; { private Dictionary&lt;string, IMessagePackSerializer&gt; _serializers; public InterfaceSerializer() : this(SerializationContext.Default) { } public InterfaceSerializer(SerializationContext context) { _serializers = new Dictionary&lt;string, IMessagePackSerializer&gt;(); // Get all types that implement T interface var implementingTypes = System.Reflection.Assembly .GetExecutingAssembly() .DefinedTypes .Where(t =&gt; t.ImplementedInterfaces.Contains(typeof(T))); // Create serializer for each type and store it in dictionary foreach (var type in implementingTypes) { var key = type.Name; var value = MessagePackSerializer.Create(type, context); _serializers.Add(key, value); } } protected override void PackToCore(Packer packer, T objectTree) { IMessagePackSerializer serializer; string typeName = objectTree.GetType().Name; // Find matching serializer if (!_serializers.TryGetValue(typeName, out serializer)) { throw SerializationExceptions.NewTypeCannotSerialize(typeof(T)); } packer.PackArrayHeader(2); // Two-element array: packer.PackString(typeName); // 0: Type name serializer.PackTo(packer, objectTree); // 1: Packed object } protected override T UnpackFromCore(Unpacker unpacker) { IMessagePackSerializer serializer; string typeName; // Read type name and packed object if (!(unpacker.ReadString(out typeName) &amp;&amp; unpacker.Read())) { throw SerializationExceptions.NewUnexpectedEndOfStream(); } // Find matching serializer if (!_serializers.TryGetValue(typeName, out serializer)) { throw SerializationExceptions.NewTypeCannotDeserialize(typeof(T)); } // Unpack and return return (T)serializer.UnpackFrom(unpacker); } } </code></pre> <p>You need to register custom serializers for interfaces you want. For example, with default serialization context you do:</p> <pre><code>SerializationContext.Default.Serializers .Register&lt;ISegment&gt;(new InterfaceSerializer&lt;ISegment&gt;()); </code></pre> <p>Since the serialzer was registered, objects containing ISegment can be packed and unpacked as usual.</p> <pre><code>var packer = MessagePackSerializer.Create&lt;PathRequest&gt;(); packer.Pack(stream, somePathRequest); stream.Position = 0; var unpackedPathRequest = packer.Unpack(stream); </code></pre>
    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