Exemplo n.º 1
0
        /// <summary>
        /// Writes the specified structural object, including references objects.
        /// </summary>
        /// <param name="obj">The object to write.</param>
        /// <param name="objSerializeType">The <see cref="Duality.Serialization.SerializeType"/> describing the object.</param>
        /// <param name="id">The objects id.</param>
        protected void WriteStruct(object obj, SerializeType objSerializeType, uint id = 0)
        {
            ISerializable objAsCustom  = obj as ISerializable;
            ISurrogate    objSurrogate = this.GetSurrogateFor(objSerializeType.Type);

            // Write the structs data type
            this.writer.Write(objSerializeType.TypeString);
            this.writer.Write(id);
            this.writer.Write(objAsCustom != null);
            this.writer.Write(objSurrogate != null);

            if (objSurrogate != null)
            {
                objSurrogate.RealObject = obj;
                objAsCustom             = objSurrogate.SurrogateObject;

                CustomSerialIO customIO = new CustomSerialIO();
                try { objSurrogate.WriteConstructorData(customIO); }
                catch (Exception e) { this.LogCustomSerializationError(id, objSerializeType.Type, e); }
                customIO.Serialize(this);
            }

            if (objAsCustom != null)
            {
                CustomSerialIO customIO = new CustomSerialIO();
                try { objAsCustom.WriteData(customIO); }
                catch (Exception e) { this.LogCustomSerializationError(id, objSerializeType.Type, e); }
                customIO.Serialize(this);
            }
            else
            {
                // Assure the type data layout has bee written (only once per file)
                this.WriteTypeDataLayout(objSerializeType);

                // Write omitted field bitmask
                bool[] fieldOmitted = new bool[objSerializeType.Fields.Length];
                for (int i = 0; i < fieldOmitted.Length; i++)
                {
                    fieldOmitted[i] = this.IsFieldBlocked(objSerializeType.Fields[i], obj);
                }
                this.WriteArrayData(fieldOmitted);

                // Write the structs fields
                for (int i = 0; i < objSerializeType.Fields.Length; i++)
                {
                    if (fieldOmitted[i])
                    {
                        continue;
                    }
                    this.WriteObjectData(objSerializeType.Fields[i].GetValue(obj));
                }
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// Writes the specified structural object, including references objects.
        /// </summary>
        /// <param name="obj">The object to write.</param>
        /// <param name="objSerializeType">The <see cref="Duality.Serialization.SerializeType"/> describing the object.</param>
        /// <param name="id">The objects id.</param>
        protected void WriteStruct(object obj, SerializeType objSerializeType, uint id = 0)
        {
            ISerializable objAsCustom  = obj as ISerializable;
            ISurrogate    objSurrogate = this.GetSurrogateFor(objSerializeType.Type);

            // Write the structs data type
            this.writer.WriteAttributeString("type", objSerializeType.TypeString);
            if (id != 0)
            {
                this.writer.WriteAttributeString("id", XmlConvert.ToString(id));
            }
            if (objAsCustom != null)
            {
                this.writer.WriteAttributeString("custom", XmlConvert.ToString(true));
            }
            if (objSurrogate != null)
            {
                this.writer.WriteAttributeString("surrogate", XmlConvert.ToString(true));
            }

            if (objSurrogate != null)
            {
                objSurrogate.RealObject = obj;
                objAsCustom             = objSurrogate.SurrogateObject;

                CustomSerialIO customIO = new CustomSerialIO(CustomSerialIO.HeaderElement);
                try { objSurrogate.WriteConstructorData(customIO); }
                catch (Exception e) { this.LogCustomSerializationError(id, objSerializeType.Type, e); }
                customIO.Serialize(this);
            }

            if (objAsCustom != null)
            {
                CustomSerialIO customIO = new CustomSerialIO(CustomSerialIO.BodyElement);
                try { objAsCustom.WriteData(customIO); }
                catch (Exception e) { this.LogCustomSerializationError(id, objSerializeType.Type, e); }
                customIO.Serialize(this);
            }
            else
            {
                // Write the structs fields
                foreach (FieldInfo field in objSerializeType.Fields)
                {
                    if (this.IsFieldBlocked(field, obj))
                    {
                        continue;
                    }
                    this.WriteObjectData(field.GetValue(obj), field.Name);
                }
            }
        }