Esempio n. 1
0
        public static IReadablePacket ReadPacketizableInto <T>([NotNull] this IReadablePacket packet, [NotNull] T result)
            where T : class
        {
            // We need something to write to.
            if (result == null)
            {
                throw new ArgumentNullException("result", "Cannot depacketize into null reference.");
            }

            // Make sure we can depacketize to this type.
            System.Diagnostics.Debug.Assert(IsPacketizable(result));

            // See if we have anything at all, or if the written value was null.
            if (packet.ReadBoolean())
            {
                // Perform the actual deserialization.
                try
                {
                    GetDepacketizer(result.GetType())(packet, result);
                }
                catch (Exception ex)
                {
                    throw new PacketException("Failed deserializing packetizable", ex);
                }
                return(packet);
            }
            throw new InvalidOperationException("Cannot read 'null' into existing instance.");
        }
Esempio n. 2
0
        public static T ReadPacketizableWithTypeInfo <T>([NotNull] this IReadablePacket packet) where T : class
        {
            // See if we have anything at all, or if the written value was null.
            if (packet.ReadBoolean())
            {
                // Get the type of whatever it is we will be reading.
                var type = packet.ReadType();

                // Create a new instance into which we then perform the actual deserialization.
                try
                {
                    var result = (T)Activator.CreateInstance(type);
                    GetDepacketizer(type)(packet, result);
                    return(result);
                }
                catch (Exception ex)
                {
                    throw new PacketException("Failed deserializing " + type.Name + " to " + typeof(T).Name, ex);
                }
            }
            return(null);
        }
Esempio n. 3
0
        public static T ReadPacketizable <T>([NotNull] this IReadablePacket packet) where T : class, new()
        {
            // Make sure we can depacketize to this type.
            System.Diagnostics.Debug.Assert(IsPacketizable <T>());

            // See if we have anything at all, or if the written value was null.
            if (packet.ReadBoolean())
            {
                // Create a new instance into which we then perform the actual deserialization.
                var result = new T();
                try
                {
                    GetDepacketizer(typeof(T))(packet, result);
                }
                catch (Exception ex)
                {
                    throw new PacketException("Failed deserializing " + typeof(T).Name, ex);
                }
                return(result);
            }
            return(null);
        }