/// <summary> /// Initializes a new instance of the <see cref="Antmicro.Migrant.ObjectWriter" /> class. /// </summary> /// <param name='stream'> /// Stream to which data will be written. /// </param> /// <param name='preSerializationCallback'> /// Callback which is called once on every unique object before its serialization. Contains this object in its only parameter. /// </param> /// <param name='postSerializationCallback'> /// Callback which is called once on every unique object after its serialization. Contains this object in its only parameter. /// </param> /// <param name='writeMethodCache'> /// Cache in which generated write methods are stored and reused between instances of <see cref="Antmicro.Migrant.ObjectWriter" />. /// Can be null if one does not want to use the cache. /// </param> /// <param name='surrogatesForObjects'> /// Dictionary, containing callbacks that provide surrogate for given type. Callbacks have to be of type Func<T, object> where /// typeof(T) is given type. /// </param> /// <param name='isGenerating'> /// True if write methods are to be generated, false if one wants to use reflection. /// </param> /// <param name = "treatCollectionAsUserObject"> /// True if collection objects are to be serialized without optimization (treated as normal user objects). /// </param> /// <param name="useBuffering"> /// True if buffering is used. False if all writes should directly go to the stream and no padding should be used. /// </param> /// <param name="referencePreservation"> /// Tells serializer how to treat object identity between the calls to <see cref="Antmicro.Migrant.ObjectWriter.WriteObject" />. /// </param> public ObjectWriter(Stream stream, Action <object> preSerializationCallback = null, Action <object> postSerializationCallback = null, IDictionary <Type, DynamicMethod> writeMethodCache = null, InheritanceAwareList <Delegate> surrogatesForObjects = null, bool isGenerating = true, bool treatCollectionAsUserObject = false, bool useBuffering = true, ReferencePreservation referencePreservation = ReferencePreservation.Preserve) { if (surrogatesForObjects == null) { surrogatesForObjects = new InheritanceAwareList <Delegate>(); } currentlyWrittenTypes = new Stack <Type>(); transientTypeCache = new Dictionary <Type, bool>(); writeMethods = new List <Action <PrimitiveWriter, object> >(); postSerializationHooks = new List <Action>(); this.writeMethodCache = writeMethodCache; this.isGenerating = isGenerating; this.treatCollectionAsUserObject = treatCollectionAsUserObject; this.surrogatesForObjects = surrogatesForObjects; typeIndices = new Dictionary <Type, int>(); methodIndices = new Dictionary <MethodInfo, int>(); this.preSerializationCallback = preSerializationCallback; this.postSerializationCallback = postSerializationCallback; writer = new PrimitiveWriter(stream, useBuffering); typeStamper = new TypeStamper(writer, treatCollectionAsUserObject); inlineWritten = new HashSet <int>(); this.referencePreservation = referencePreservation; if (referencePreservation == ReferencePreservation.Preserve) { identifier = new ObjectIdentifier(); } }
private void PrepareForNextWrite() { if (writer != null) { writer.Dispose(); } identifier = new ObjectIdentifier(); writer = new PrimitiveWriter(stream); typeStamper = new TypeStamper(writer); inlineWritten = new HashSet <int>(); }