Esempio n. 1
0
        /// <summary>
        /// Deserializes the specified serialized value.
        /// </summary>
        /// <typeparam name="T">
        /// The type of the value to deserialize.
        /// </typeparam>
        /// <param name="serialized">The serialized value.</param>
        /// <returns>
        /// The value.
        /// </returns>
        /// <exception cref="ProtocolSerializerException">
        /// Thrown when initialization fails.
        /// </exception>
        public static T Deserialize <T>(byte[] serialized)
        {
            using (var ms = new MemoryStream(serialized))
            {
                if (ProtocolSerializerImplementation <T> .InitializeError != null)
                {
                    throw new ProtocolSerializerException(ProtocolSerializerImplementation <T> .InitializeError);
                }

                return(ProtocolSerializerImplementation <T> .Read(ms));
            }
        }
Esempio n. 2
0
        /// <summary>
        /// Serializes the specified value.
        /// </summary>
        /// <typeparam name="T">
        /// The type of the value.
        /// </typeparam>
        /// <param name="value">The value.</param>
        /// <returns>The value serialized in a byte arry.</returns>
        /// <exception cref="ProtocolSerializerException">
        /// Thrown when initialization fails.
        /// </exception>
        public static byte[] Serialize <T>(T value)
        {
            using (var ms = new MemoryStream())
            {
                if (ProtocolSerializerImplementation <T> .InitializeError != null)
                {
                    throw new ProtocolSerializerException(ProtocolSerializerImplementation <T> .InitializeError);
                }

                ProtocolSerializerImplementation <T> .Write(ms, value);

                return(ms.ToArray());
            }
        }