Note that there are some explanatory texts on larger screens.

plurals
  1. POReceiving serialized custom message types over a network, how to cast?
    primarykey
    data
    text
    <p>I'm writing a networked application.</p> <p>Assume a class called <strong>Packet</strong>. This class has some public properties.</p> <pre><code>public class Packet { public Module DestinationModule { get; set; } public EncryptionCompressionFlag EncryptedOrCompressed { get; set; } public PacketPriority Priority { get; set; } public Origin Origin { get; set; } // You'll see why I need this later // This StackOverflow question is asking how I can do without this property public Type UltimateType { get; set; } } </code></pre> <p>Now assume a class called <strong>LoginPacket</strong>. This class extends <strong>Packet</strong>, and includes even more properties.</p> <pre><code>public class LoginPacket : Packet { public string Username { get; set; } public string Password { get; set; } } </code></pre> <p>But <strong>LoginPacket</strong> is not the only type of custom packet. There are many more custom packets.</p> <p>My client application is constantly serializing custom packets and sending them over the wire. When they are deserialized on the other side, however, I can only deserialize them as a Packet, not as a specific <strong>LoginPacket</strong>. </p> <p>Question: How can I know the 'ultimate' or 'original' specific type of the Packet? Per the example, the specific type would have been <strong>LoginPacket</strong>. I don't know the specific type because I receive it over the wire simply as a <strong>Packet</strong> and without a "type" property (like <em>UltimateType</em>, which sounds lame), I don't know how to discover the specific original type.</p> <p><em>Response: "How do you serialize the packet?"</em></p> <p>I use <a href="http://www.codeproject.com/KB/XML/yaxlib.aspx" rel="nofollow">YAXSerializer</a>. I tested a specific serialization and deserialization of <strong>LoginPacket</strong>, and it was successful. I was able to deserialize the string (<em>YAX serializes to a string, not byte array, difference doesn't really matter..</em>) into a <strong>Packet</strong> <em>and</em> a <strong>LoginPacket</strong>. </p> <pre><code> public static string SerializePacket(Packet packet) { YAXSerializer serializer = new YAXSerializer(packet.UltimateType); return serializer.Serialize(packet); } </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.
 

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