Ejemplo n.º 1
0
        }                                                 // Implement as required by overriding

        protected MemoryStream SerialiseToMemory <T>(T inputObj)
        {
            var stream = new MemoryStream();

            serialiser.SerializeWithLengthPrefix(stream, inputObj, typeof(T), ProtoBuf.PrefixStyle.Base128, 0);
            stream.Seek(0, SeekOrigin.Begin);
            return(stream);
        }
Ejemplo n.º 2
0
        /// <summary>
        ///     Serialises a data transfer object (DTO) of type <typeparamref name="T"/> into
        ///     an <paramref name="output"/> stream, optionally with a Base128 length prefix.
        /// </summary>
        /// <remarks>
        ///     Provides deserialisation capabilities for any object which derives from
        ///     <see cref="IDataTransferObject"/> and that has a <see cref="ProtoContractAttribute"/>
        ///     attribute (e.g. those from ObscurCore.DTO namespace).
        /// </remarks>
        /// <typeparam name="T">The type of object to serialise.</typeparam>
        /// <param name="obj">The <typeparamref name="T"/> to serialise.</param>
        /// <param name="output">The stream to write the serialised <typeparamref name="T"/> to.</param>
        /// <param name="prefixLength">
        ///     If <c>true</c>, the serialised object will be prefixed with its length in Base128 format.
        ///     Use when recipient does not know data length, e.g. networked communications, or streams
        ///     with multiple serialised objects.
        /// </param>
        public static void SerialiseDataTransferObject <T>(T obj, Stream output, bool prefixLength = false)
            where T : IDataTransferObject
        {
            Type type = typeof(T);

            if (Serialiser.CanSerializeContractType(type) == false)
            {
                throw new ArgumentException(
                          "Cannot serialise - object type does not have a serialisation contract.", "obj");
            }
            if (prefixLength)
            {
                Serialiser.SerializeWithLengthPrefix(output, obj, type, PrefixStyle.Base128, 0);
            }
            else
            {
                Serialiser.Serialize(output, obj);
            }
        }