private void PrepareForTheRead() { if (reader != null) { reader.Dispose(); } delegatesCache = new Dictionary <Type, Func <int, object> >(); deserializedObjects = new AutoResizingList <object>(InitialCapacity); reader = new PrimitiveReader(stream); stamper = new TypeStampReader(reader, versionToleranceLevel); }
public ReadMethodGenerator(Type typeToGenerate, TypeStampReader stampReader) { this.stampReader = stampReader; if (typeToGenerate.IsArray) { dynamicMethod = new DynamicMethod("Read", typeof(object), ParameterTypes, true); } else { dynamicMethod = new DynamicMethod("Read", typeof(object), ParameterTypes, typeToGenerate, true); } generator = dynamicMethod.GetILGenerator(); GenerateDynamicCode(typeToGenerate); }
/// <summary> /// Initializes a new instance of the <see cref="Antmicro.Migrant.ObjectReader" /> class. /// </summary> /// <param name='stream'> /// Stream from which objects will be read. /// </param> /// <param name='objectsForSurrogates'> /// Dictionary, containing callbacks that provide objects for given type of surrogate. Callbacks have to be of type Func<T, object> where /// typeof(T) is type of surrogate. /// </param> /// <param name='postDeserializationCallback'> /// Callback which will be called after deserialization of every unique object. Deserialized /// object is given in the callback's only parameter. /// </param> /// <param name='readMethods'> /// Cache in which generated read methods are stored and reused between instances of <see cref="Antmicro.Migrant.ObjectReader" />. /// Can be null if one does not want to use the cache. /// </param> /// <param name='isGenerating'> /// True if read methods are to be generated, false if one wants to use reflection. /// </param> /// <param name = "treatCollectionAsUserObject"> /// True if collection objects are to be deserialized without optimization (treated as normal user objects). /// </param> /// <param name="versionToleranceLevel"> /// Describes the tolerance level of this reader when handling discrepancies in type description (new or missing fields, etc.). /// </param> /// <param name="useBuffering"> /// True if buffering was used with the corresponding ObjectWriter or false otherwise - i.e. when no padding and buffering is used. /// </param> /// <param name="referencePreservation"> /// Tells deserializer whether open stream serialization preserved objects identieties between serialization. Note that this option should /// be consistent with what was used during serialization. /// </param> public ObjectReader(Stream stream, InheritanceAwareList <Delegate> objectsForSurrogates = null, Action <object> postDeserializationCallback = null, IDictionary <Type, DynamicMethod> readMethods = null, bool isGenerating = false, bool treatCollectionAsUserObject = false, VersionToleranceLevel versionToleranceLevel = 0, bool useBuffering = true, ReferencePreservation referencePreservation = ReferencePreservation.Preserve) { if (objectsForSurrogates == null) { objectsForSurrogates = new InheritanceAwareList <Delegate>(); } this.objectsForSurrogates = objectsForSurrogates; this.readMethodsCache = readMethods ?? new Dictionary <Type, DynamicMethod>(); this.useGeneratedDeserialization = isGenerating; typeList = new List <Type>(); methodList = new List <MethodInfo>(); postDeserializationHooks = new List <Action>(); this.postDeserializationCallback = postDeserializationCallback; this.treatCollectionAsUserObject = treatCollectionAsUserObject; delegatesCache = new Dictionary <Type, Func <int, object> >(); reader = new PrimitiveReader(stream, useBuffering); stamper = new TypeStampReader(reader, versionToleranceLevel); this.referencePreservation = referencePreservation; }