Beispiel #1
0
        /// <summary>
        ///     Gets the serializeds schema.
        /// </summary>
        private byte[] GetSerializedSchema()
        {
#if HT4O_BUFFEREDBINARYWRITER
            using (var ms = new WritableMemoryStream())
            {
                using (var binaryWriter = new BufferedBinaryWriter(ms, System.Text.Encoding.ASCII, true))
                {
                    Encoder.WriteTag(binaryWriter, Tags.TypeSchema2);
                    Encoder.WriteByte(binaryWriter, TypeSchema.Version, false);
                    Encoder.WriteCount(binaryWriter, this.Properties.Length);
                    foreach (var typeSchemaProperty in this.Properties)
                    {
                        Encoder.WriteString(binaryWriter, typeSchemaProperty.PropertyName, false);
                    }
                }

                return(ms.ToArray());
            }
#else
            using (var binaryWriter = new HeapBinaryWriter(System.Text.Encoding.ASCII)) {
                Encoder.WriteTag(binaryWriter, Tags.TypeSchema2);
                Encoder.WriteByte(binaryWriter, TypeSchema.Version, false);
                Encoder.WriteCount(binaryWriter, this.Properties.Length);
                foreach (var typeSchemaProperty in this.Properties)
                {
                    Encoder.WriteString(binaryWriter, typeSchemaProperty.PropertyName, false);
                }

                return(binaryWriter.ToArray());
            }
#endif
        }
Beispiel #2
0
        /// <summary>
        ///     Serialize an object.
        /// </summary>
        /// <param name="entityContext">
        ///     The entity context.
        /// </param>
        /// <param name="serializeType">
        ///     The serialize type.
        /// </param>
        /// <param name="value">
        ///     The object to serialize.
        /// </param>
        /// <param name="capacity">
        ///     The internal memory stream intial capacity.
        /// </param>
        /// <param name="write">
        ///     If <c>true</c> value will be serialized; otherwise only traversed, null will be returned.
        /// </param>
        /// <param name="serializingEntity">
        ///     The serializing entity delegate.
        /// </param>
        /// <returns>
        ///     The serialized object.
        /// </returns>
        internal static byte[] Serialize(EntityContext entityContext, Type serializeType, object value, int capacity, bool write,
                                         SerializingEntity serializingEntity)
        {
            if (write)
            {
#if HT4O_BUFFEREDBINARYWRITER
                using (var memoryStream = new WritableMemoryStream(capacity))
                {
                    using (var binaryWriter = new BufferedBinaryWriter(memoryStream, true))
                    {
                        var entitySerializer = new EntitySerializer(entityContext, binaryWriter, value, serializingEntity);
                        entitySerializer.Write(serializeType, value);
                    }

                    return(memoryStream.ToArray());
                }
#else
                using (var binaryWriter = new HeapBinaryWriter()) {
                    var entitySerializer = new EntitySerializer(entityContext, binaryWriter, value, serializingEntity);
                    entitySerializer.Write(serializeType, value);
                    return(binaryWriter.ToArray());
                }
#endif
            }

            using (var binaryWriter = new FakeBinaryWriter())
            {
                var entitySerializer = new EntitySerializer(entityContext, binaryWriter, value, serializingEntity);
                entitySerializer.Write(serializeType, value);
            }

            return(null);
        }