internal static void GenerateFieldsExpression(Type type, FieldInfo[] fields, ParameterExpression outputStream, Expression objToSerialize, ParameterExpression objTracking, List <ParameterExpression> variables, List <Expression> notTrackedExpressions) { var serializer = Expression.Parameter(typeof(Action <Stream, object, SerializerObjectTracker>), "serializer"); var typeExpr = Expression.Parameter(typeof(Type), "typeExpr"); var itemAsObj = Expression.Parameter(typeof(object), "itemAsObj"); variables.Add(serializer); variables.Add(typeExpr); variables.Add(itemAsObj); List <Expression> copyFieldsExpressions = new List <Expression>(); if (type.IsClass) { copyFieldsExpressions.Add(Expression.Call(objTracking, SerializerObjectTrackerMih.TrackObject(), objToSerialize)); } notTrackedExpressions.AddRange(SerializationCallbacksHelper.GenerateOnSerializingAttributeExpression(type, objToSerialize, Expression.New(StreamingContextMih.Constructor(), Expression.Constant(StreamingContextStates.All)))); foreach (var fieldInfo in fields) { var fieldValueExpr = Expression.Field(Expression.Convert(objToSerialize, type), fieldInfo); if (fieldInfo.FieldType == typeof(string)) { copyFieldsExpressions.Add(GenerateStringExpression(outputStream, fieldValueExpr, objTracking)); } else if (fieldInfo.FieldType.IsPrimitive || fieldInfo.FieldType.IsValueType) { var primitiveWriter = GetPrimitiveWriter(fieldInfo.FieldType); if (primitiveWriter == null) { var primitiveSerializer = GetTypeSerializer(fieldInfo.FieldType); copyFieldsExpressions.Add(Expression.Invoke(Expression.Constant(primitiveSerializer), outputStream, fieldValueExpr, objTracking)); } else { copyFieldsExpressions.Add(primitiveWriter(outputStream, fieldValueExpr, objTracking)); } } else { copyFieldsExpressions.Add(GetWriteClassTypeExpression(outputStream, objTracking, fieldValueExpr, itemAsObj, typeExpr, serializer, fieldInfo.FieldType)); } } notTrackedExpressions.AddRange(copyFieldsExpressions); notTrackedExpressions.AddRange(SerializationCallbacksHelper.GenerateOnSerializedAttributeExpression(type, objToSerialize, Expression.New(StreamingContextMih.Constructor(), Expression.Constant(StreamingContextStates.All)))); }
private static Expression SerializeISerializable(Type type, List <ParameterExpression> variables, ParameterExpression outputStream, ParameterExpression objToSerialize, ParameterExpression objTracking) { var fc = Expression.Parameter(typeof(FormatterConverter), "fc"); var context = Expression.Parameter(typeof(StreamingContext), "context"); var si = Expression.Parameter(typeof(SerializationInfo), "si"); var iser = Expression.Parameter(typeof(ISerializable), "iser"); variables.Add(fc); variables.Add(context); variables.Add(si); variables.Add(iser); var getEnumeratorMethodInfo = SerializationInfoMih.GetEnumerator(); var enumeratorMethod = Expression.Call(si, getEnumeratorMethodInfo); var loopBodyCargo = new EnumerableLoopBodyCargo(); loopBodyCargo.EnumeratorType = typeof(SerializationInfoEnumerator); loopBodyCargo.KvpType = typeof(SerializationEntry); var preLoopActions = new List <Expression>(); preLoopActions.Add(PrimitiveHelpers.WriteInt32(outputStream, Expression.Property(si, SerializationInfoMih.MemberCount()), objTracking)); var notTrackedExpressions = new List <Expression>(); notTrackedExpressions.Add(PrimitiveHelpers.WriteByte(outputStream, Expression.Constant(SerializerObjectTracker.Value0, typeof(byte)), objTracking)); notTrackedExpressions.Add(Expression.Assign(fc, Expression.New(typeof(FormatterConverter)))); notTrackedExpressions.Add(Expression.Assign(context, Expression.New(StreamingContextMih.Constructor(), Expression.Constant(StreamingContextStates.All)))); notTrackedExpressions.Add(Expression.Assign(si, Expression.New(SerializationInfoMih.Constructor(), Expression.Constant(type), fc))); notTrackedExpressions.Add(Expression.Assign(iser, Expression.Convert(objToSerialize, typeof(ISerializable)))); notTrackedExpressions.AddRange(SerializationCallbacksHelper.GenerateOnSerializingAttributeExpression(type, objToSerialize, context)); notTrackedExpressions.Add(Expression.Call(iser, ISerializableMih.GetObjectData(), si, context)); notTrackedExpressions.AddRange(SerializationCallbacksHelper.GenerateOnSerializedAttributeExpression(type, objToSerialize, context)); notTrackedExpressions.Add(Expression.IfThen(Expression.IsTrue(Expression.Property(si, "IsFullTypeNameSetExplicit")), Expression.Throw(Expression.New(InvalidOperationExceptionMih.Constructor(), Expression.Constant("Changing the full type name for an ISerializable is not supported"))))); notTrackedExpressions.Add(Expression.IfThen(Expression.IsTrue(Expression.Property(si, "IsAssemblyNameSetExplicit")), Expression.Throw(Expression.New(InvalidOperationExceptionMih.Constructor(), Expression.Constant("Changing the assembly name for an ISerializable is not supported"))))); notTrackedExpressions.Add(EnumerableLoopHelper.GenerateEnumeratorLoop(variables, DictionarySerializer.GetKeyValuePairLoopBodyCargo(outputStream, objTracking, "Name", typeof(string), "Value", typeof(object)), enumeratorMethod, preLoopActions, loopBodyCargo)); notTrackedExpressions.Add(Expression.Call(objTracking, SerializerObjectTrackerMih.TrackObject(), objToSerialize)); return(Expression.Block(notTrackedExpressions)); }
/// <summary> /// Generates an expression tree to clone an ISerializable /// </summary> /// <param name="variables">Global variables for the expression tree</param> /// <param name="source">Source object</param> /// <param name="clone">Clone object</param> /// <param name="sourceType">Type of the source object</param> /// <param name="refTrackerParam">Reference tracker</param> /// <returns>An expression tree to clone an ISerializable</returns> public static Expression GenerateISerializableExpression(List <ParameterExpression> variables, ParameterExpression source, ParameterExpression clone, Type sourceType, ParameterExpression refTrackerParam) { var serializationConstructor = ISerializableSerializer.GetSerializationConstructor(sourceType); if (serializationConstructor == null) { throw new MissingConstructorException("Cannot serialize type " + sourceType + " because it does not have the required constructor for ISerializable. If you inherits from a class that implements ISerializable you have to expose the serialization constructor."); } var getEnumeratorMethodInfo = SerializationInfoMih.GetEnumerator(); var siSource = Expression.Parameter(typeof(SerializationInfo), "siSource"); var fc = Expression.Parameter(typeof(FormatterConverter), "fc"); var context = Expression.Parameter(typeof(StreamingContext), "context"); var iserSource = Expression.Parameter(typeof(ISerializable), "iserSource"); var siClone = Expression.Parameter(typeof(SerializationInfo), "siClone"); var cloner = Expression.Parameter(typeof(Func <object, ObjectClonerReferenceTracker, object>), "cloner"); var clonedItem = Expression.Parameter(typeof(object), "clonedItem"); variables.Add(fc); variables.Add(context); variables.Add(siSource); variables.Add(iserSource); variables.Add(siClone); variables.Add(cloner); variables.Add(clonedItem); var enumeratorMethod = Expression.Call(siSource, getEnumeratorMethodInfo); var loopBodyCargo = new EnumerableLoopBodyCargo(); loopBodyCargo.EnumeratorType = typeof(SerializationInfoEnumerator); loopBodyCargo.KvpType = typeof(SerializationEntry); var expressions = new List <Expression>(); expressions.Add(Expression.Assign(fc, Expression.New(typeof(FormatterConverter)))); expressions.Add(Expression.Assign(context, Expression.New(StreamingContextMih.Constructor(), Expression.Constant(StreamingContextStates.All)))); expressions.Add(Expression.Assign(siSource, Expression.New(SerializationInfoMih.Constructor(), Expression.Constant(sourceType), fc))); expressions.Add(Expression.Assign(iserSource, Expression.Convert(source, typeof(ISerializable)))); expressions.Add(Expression.Assign(siClone, Expression.New(SerializationInfoMih.Constructor(), Expression.Constant(sourceType), fc))); expressions.AddRange(SerializationCallbacksHelper.GenerateOnSerializingAttributeExpression(sourceType, source, context)); expressions.Add(Expression.Call(iserSource, ISerializableMih.GetObjectData(), siSource, context)); expressions.AddRange(SerializationCallbacksHelper.GenerateOnSerializedAttributeExpression(sourceType, source, context)); expressions.Add(Expression.IfThen(Expression.IsTrue(Expression.Property(siSource, "IsFullTypeNameSetExplicit")), Expression.Throw(Expression.New(InvalidOperationExceptionMih.Constructor(), Expression.Constant("Changing the full type name for an ISerializable is not supported"))))); expressions.Add(Expression.IfThen(Expression.IsTrue(Expression.Property(siSource, "IsAssemblyNameSetExplicit")), Expression.Throw(Expression.New(InvalidOperationExceptionMih.Constructor(), Expression.Constant("Changing the assembly name for an ISerializable is not supported"))))); expressions.Add(EnumerableLoopHelper.GenerateEnumeratorLoop(variables, GetLoopBodyCargo(siClone, clonedItem, refTrackerParam), enumeratorMethod, null, loopBodyCargo)); expressions.Add(Expression.Assign(clone, Expression.New(serializationConstructor, siClone, context))); expressions.Add(Expression.Call(refTrackerParam, ObjectClonerReferenceTrackerMih.Track(), source, clone)); expressions.AddRange(SerializationCallbacksHelper.GenerateOnDeserializedAttributeExpression(sourceType, clone, context)); expressions.Add(SerializationCallbacksHelper.GenerateCallIDeserializationExpression(sourceType, clone)); return(ObjectCloner.GenerateNullTrackedOrUntrackedExpression(source, clone, sourceType, refTrackerParam, Expression.Block(expressions))); }