/// <summary> /// Generates an expression tree to handle class type serialization /// </summary> /// <param name="outputStream">Stream to write to</param> /// <param name="objTracking">Reference tracker</param> /// <param name="item">Source object</param> /// <param name="itemAsObj">Source object as an object</param> /// <param name="typeExpr">Type of the object as an expression</param> /// <param name="serializer">Serializer temporary variable</param> /// <param name="itemType">Type of the element</param> /// <returns>An expression tree to handle class type serialization</returns> internal static Expression GetWriteClassTypeExpression(ParameterExpression outputStream, ParameterExpression objTracking, Expression item, Expression itemAsObj, ParameterExpression typeExpr, ParameterExpression serializer, Type itemType) { var writeType = Expression.IfThenElse(Expression.Equal(Expression.Call(item, ObjectMih.GetTypeMethod()), Expression.Constant(itemType)), Expression.Block(PrimitiveHelpers.WriteByte(outputStream, Expression.Constant(SerializerObjectTracker.Value0), objTracking), Expression.Assign(typeExpr, Expression.Constant(itemType)), Expression.Assign(itemAsObj, Expression.Convert(item, typeof(object)))), Expression.Block(PrimitiveHelpers.WriteByte(outputStream, Expression.Constant(SerializerObjectTracker.Value1), objTracking), Expression.Assign(itemAsObj, Expression.Call(SerializerMih.PrepareObjectForSerialization(), item)), Expression.Assign(typeExpr, Expression.Call(itemAsObj, ObjectMih.GetTypeMethod())), GenerateStringExpression(outputStream, Expression.Call(SerializedTypeResolverMih.GetShortNameFromType(), typeExpr), objTracking), PrimitiveHelpers.WriteInt32(outputStream, Expression.Call(SerializedTypeResolverMih.GetHashCodeFromType(), typeExpr), objTracking))); return(Expression.IfThenElse(Expression.Equal(item, Expression.Constant(null)), PrimitiveHelpers.WriteByte(outputStream, Expression.Constant(SerializerObjectTracker.Value0), objTracking), Expression.Block(PrimitiveHelpers.WriteByte(outputStream, Expression.Constant(SerializerObjectTracker.Value1), objTracking), writeType, Expression.Assign(serializer, Expression.Call(SerializerMih.GetTypeToObjectSerializer(), typeExpr)), Expression.Invoke(serializer, outputStream, itemAsObj, objTracking)))); }
/// <summary> /// Generate an expression tree to deserialize a class /// </summary> /// <param name="inputStream">Stream to read from</param> /// <param name="objTracker">Reference tracker</param> /// <param name="leftSide">Left side of the assignment</param> /// <param name="typeExpr">Type of the class as an expression</param> /// <param name="typeName">Type of the class as a string</param> /// <param name="typeHashCode">Hashcode of the class</param> /// <param name="deserializer">Temporary deserializer variable</param> /// <param name="itemType">Type of the class</param> /// <returns>An expression tree to deserialize a class</returns> internal static Expression GetReadClassExpression(ParameterExpression inputStream, ParameterExpression objTracker, Expression leftSide, ParameterExpression typeExpr, ParameterExpression typeName, ParameterExpression typeHashCode, ParameterExpression deserializer, Type itemType) { var readType = Expression.IfThenElse(Expression.Equal(Expression.Convert(PrimitiveHelpers.ReadByte(inputStream, objTracker), typeof(byte)), Expression.Constant(SerializerObjectTracker.Value0)), Expression.Assign(typeExpr, Expression.Call(SerializedTypeResolverMih.GetTypeFromFullName_Type(), Expression.Constant(itemType))), Expression.Block(Expression.Assign(typeName, Deserializer.GenerateStringExpression(inputStream, objTracker)), Expression.Assign(typeHashCode, PrimitiveHelpers.ReadInt32(inputStream, objTracker)), Expression.Assign(typeExpr, Expression.Call(SerializedTypeResolverMih.GetTypeFromFullName_String(), typeName)), Expression.IfThen(Expression.NotEqual(typeHashCode, Expression.Property(typeExpr, "HashCode")), Expression.Throw(Expression.New(TypeWasModifiedSinceSerializationException.GetConstructor(), typeExpr))))); var invokeDeserializer = Expression.Invoke(deserializer, inputStream, objTracker); Expression convertExpression = Expression.Convert(Expression.Call(SerializerMih.ConvertObjectToExpectedType(), invokeDeserializer, Expression.Constant(itemType)), itemType); return(Expression.IfThenElse(Expression.Equal(Expression.Convert(PrimitiveHelpers.ReadByte(inputStream, objTracker), typeof(byte)), Expression.Constant(SerializerObjectTracker.Value0)), Expression.Assign(leftSide, Expression.Constant(null, itemType)), Expression.Block( readType, Expression.Assign(deserializer, Expression.Call(DeserializerMih.GetTypeToObjectDeserializer(), Expression.Property(typeExpr, "Type"))), Expression.Assign(leftSide, convertExpression)))); }