private void WriteStruct(object obj, ObjectHeader header) { ISerializeExplicit objAsCustom = obj as ISerializeExplicit; ISerializeSurrogate objSurrogate = GetSurrogateFor(header.ObjectType); 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(header.ObjectId, header.ObjectType, e); } customIO.Serialize(this); } if (objAsCustom != null) { CustomSerialIO customIO = new CustomSerialIO(); try { objAsCustom.WriteData(customIO); } catch (Exception e) { this.LogCustomSerializationError(header.ObjectId, header.ObjectType, e); } customIO.Serialize(this); } else { // Assure the type data layout has been written (only once per file) this.WriteTypeDataLayout(header.SerializeType); // If we're serializing a value type, skip the entire object body if // it equals the zero-init struct. This will keep struct-heavy data a lot // smaller binary-wise. bool skipEntireBody = header.ObjectType.IsValueType && object.Equals(obj, header.SerializeType.DefaultValue); // Write omitted field bitmask bool[] fieldOmitted = new bool[header.SerializeType.Fields.Length]; for (int i = 0; i < fieldOmitted.Length; i++) { fieldOmitted[i] = skipEntireBody || this.IsFieldBlocked(header.SerializeType.Fields[i], obj); } this.WriteArrayData(fieldOmitted); // Write the structs fields for (int i = 0; i < header.SerializeType.Fields.Length; i++) { if (fieldOmitted[i]) { continue; } this.WriteObjectData(header.SerializeType.Fields[i].GetValue(obj)); } } }
private void WriteStruct(XElement element, object obj, ObjectHeader header) { ISerializeExplicit objAsCustom = obj as ISerializeExplicit; ISerializeSurrogate objSurrogate = GetSurrogateFor(header.ObjectType); // Write the structs data type if (objAsCustom != null) { element.SetAttributeValue("custom", XmlConvert.ToString(true)); } if (objSurrogate != null) { element.SetAttributeValue("surrogate", XmlConvert.ToString(true)); } if (objSurrogate != null) { objSurrogate.RealObject = obj; objAsCustom = objSurrogate.SurrogateObject; CustomSerialIO customIO = new CustomSerialIO(); try { objSurrogate.WriteConstructorData(customIO); } catch (Exception e) { this.LogCustomSerializationError(header.ObjectId, header.ObjectType, e); } XElement customHeaderElement = new XElement(CustomSerialIO.HeaderElement); element.Add(customHeaderElement); customIO.Serialize(this, customHeaderElement); } if (objAsCustom != null) { CustomSerialIO customIO = new CustomSerialIO(); try { objAsCustom.WriteData(customIO); } catch (Exception e) { this.LogCustomSerializationError(header.ObjectId, header.ObjectType, e); } XElement customBodyElement = new XElement(CustomSerialIO.BodyElement); element.Add(customBodyElement); customIO.Serialize(this, customBodyElement); } else { // Write the structs fields foreach (FieldInfo field in header.SerializeType.Fields) { if (this.IsFieldBlocked(field, obj)) { continue; } XElement fieldElement = new XElement(GetXmlElementName(field.Name)); element.Add(fieldElement); this.WriteObjectData(fieldElement, field.GetValue(obj)); } } }
private void WriteStruct(object obj, ObjectHeader header) { ISerializeExplicit objAsCustom = obj as ISerializeExplicit; ISerializeSurrogate objSurrogate = GetSurrogateFor(header.ObjectType); 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(header.ObjectId, header.ObjectType, e); } customIO.Serialize(this); } if (objAsCustom != null) { CustomSerialIO customIO = new CustomSerialIO(); try { objAsCustom.WriteData(customIO); } catch (Exception e) { this.LogCustomSerializationError(header.ObjectId, header.ObjectType, e); } customIO.Serialize(this); } else { // Assure the type data layout has been written (only once per file) this.WriteTypeDataLayout(header.SerializeType); // Write omitted field bitmask bool[] fieldOmitted = new bool[header.SerializeType.Fields.Length]; for (int i = 0; i < fieldOmitted.Length; i++) { fieldOmitted[i] = this.IsFieldBlocked(header.SerializeType.Fields[i], obj); } this.WriteArrayData(fieldOmitted); // Write the structs fields for (int i = 0; i < header.SerializeType.Fields.Length; i++) { if (fieldOmitted[i]) { continue; } this.WriteObjectData(header.SerializeType.Fields[i].GetValue(obj)); } } }
private void WriteStruct(XElement element, object obj, ObjectHeader header) { ISerializeExplicit objAsCustom = obj as ISerializeExplicit; ISerializeSurrogate objSurrogate = GetSurrogateFor(header.ObjectType); // If we're serializing a value type, skip the entire object body if // it equals the zero-init struct. This will keep struct-heavy data a lot // more concise. if (header.ObjectType.IsValueType && object.Equals(obj, header.SerializeType.DefaultValue)) { return; } // Write information about custom or surrogate serialization if (objAsCustom != null) { element.SetAttributeValue("custom", XmlConvert.ToString(true)); } if (objSurrogate != null) { element.SetAttributeValue("surrogate", XmlConvert.ToString(true)); } if (objSurrogate != null) { objSurrogate.RealObject = obj; objAsCustom = objSurrogate.SurrogateObject; CustomSerialIO customIO = new CustomSerialIO(); try { objSurrogate.WriteConstructorData(customIO); } catch (Exception e) { this.LogCustomSerializationError(header.ObjectId, header.ObjectType, e); } XElement customHeaderElement = new XElement(CustomSerialIO.HeaderElement); element.Add(customHeaderElement); customIO.Serialize(this, customHeaderElement); } if (objAsCustom != null) { CustomSerialIO customIO = new CustomSerialIO(); try { objAsCustom.WriteData(customIO); } catch (Exception e) { this.LogCustomSerializationError(header.ObjectId, header.ObjectType, e); } XElement customBodyElement = new XElement(CustomSerialIO.BodyElement); element.Add(customBodyElement); customIO.Serialize(this, customBodyElement); } else { // Write the structs fields foreach (FieldInfo field in header.SerializeType.Fields) { if (this.IsFieldBlocked(field, obj)) { continue; } XElement fieldElement = new XElement(GetXmlElementName(field.Name)); element.Add(fieldElement); this.WriteObjectData(fieldElement, field.GetValue(obj)); } } }