Beispiel #1
0
        public static IWritablePacket Write <T>([NotNull] this IWritablePacket packet, T data) where T : class
        {
            // Check whether we have something.
            if (data != null)
            {
                // Check its underlying type.
                var type = data.GetType();
                if (!IsPacketizable(type))
                {
                    return(packet);
                }

                // Flag that we have something.
                packet.Write(true);

                // Packetize all fields, then give the object a chance to do manual
                // serialization, e.g. of collections and such.
                try
                {
                    GetPacketizer(type)(packet, data);
                }
                catch (Exception ex)
                {
                    throw new PacketException("Failed serializing " + type.Name, ex);
                }
                return(packet);
            }

            // Flag that we have nothing.
            return(packet.Write(false));
        }
Beispiel #2
0
        public static IWritablePacket WriteWithTypeInfo <T>([NotNull] this IWritablePacket packet, T data)
            where T : class
        {
            // Check whether we have something.
            if (data != null)
            {
                // Check its underlying type.
                var type = data.GetType();
                if (!IsPacketizable(type))
                {
                    return(packet);
                }

                // Flag that we have something.
                packet.Write(true);

                // Make sure we have a parameterless public constructor for deserialization.
                System.Diagnostics.Debug.Assert(type.GetConstructor(BindingFlags.Public | BindingFlags.Instance, null, Type.EmptyTypes, null) != null);

                // Store the type, which also tells us the value us not null.
                packet.Write(type);

                // Packetize all fields, then give the object a chance to do manual
                // serialization, e.g. of collections and such.
                try
                {
                    GetPacketizer(type)(packet, data);
                }
                catch (Exception ex)
                {
                    throw new PacketException("Failed serializing " + type.Name, ex);
                }
                return(packet);
            }

            // Flag that we have nothing.
            return(packet.Write(false));
        }